readonly icon indicating copy to clipboard operation
readonly copied to clipboard

Readonly silently does nothing on blessed refs

Open scottj97 opened this issue 11 years ago • 1 comments

The docs don't really specify what happens if the scalar passed to Readonly::Scalar is a blessed object. I expected it to behave the same way as an unblessed array ref, but that does not seem to be the case:

package MyClass;
sub new { bless [], 'MyClass'; }
sub add { push @{ $_[0] }, $_[1]; $_[0] }
sub print { printf "I have %d values: %s\n", scalar @{ $_[0] }, join(q{,}, @{ $_[0] }); }

package main;
use 5.012;
use Readonly;
printf "Perl %s using Readonly version %s with XSokay=%d\n", $], $Readonly::VERSION, $Readonly::XSokay;

my $foo = MyClass->new()->add(1)->add(2);
$foo->print();

Readonly::Scalar my $rof => $foo;

$rof->add(4)->print(); # Why is this allowed to change $rof?

On my system this prints:

Perl 5.012000 using Readonly version 1.05 with XSokay=1
I have 2 values: 1,2
I have 3 values: 1,2,4

I'm using the latest Readonly zip from github, though probably an old Readonly::XS.

If this is not expected to work, consider this a documentation request. :)

scottj97 avatar Apr 24 '14 23:04 scottj97

I wasn't ignoring this, just... not 100% sure what to do with blessed objects besides document that they aren't handled by Readonly. I really like the idea of being able to lock down objects but between Moose, et al., and all the hand rolled object styles out there, ...it's just not easily doable.

I say it'd be a major project in itself but I'd seriously consider a patch if anyone decided to spend time sorting it all out with the other major players (Moose team especially). Long shot but I'll leave this issue open until the end of the year in case someone really really has time on their hands.

And you don't need Readonly::XS anymore. Or at least you won't once I get a few more people using the unstable version in more real world environments. ;)

sanko avatar Jun 03 '14 02:06 sanko