patcat88 has asked for the wisdom of the Perl Monks concerning the following question:
readonly on#!/usr/bin/perl -w use strict; use Data::Dumper; use Scalar::Readonly ':all'; my $count = 0; my @savearr = (); eval { sub make { save('begin '.$count++.' end'); } sub save { readonly_on($_[0]); #comment this out push(@savearr, \$_[0]); } for(0..3) { make(); } print(Dumper(\@savearr)); }; if($@) { print("failed in eval with\n\"$@\"\n".Dumper(\@savearr)); }
readonly offC:\Documents and Settings\Owner\Desktop>perl ro.pl failed in eval with "Modification of a read-only value attempted at ro.pl line 9. " $VAR1 = [ \"begin 0 end" ]; C:\Documents and Settings\Owner\Desktop>
This is the Perl equivalent of an XS problem I am having. If I turn on readonly, on the next loop run it dies. But this is a second run, with a new value. Old value when out of scope (but still lives in the @savearr). If I comment out the readonly, and it reaches the end, you will see 4 references, to the same, or is that different scalar? Yet the readonly flag proves that its the same scalar and it stays around AFTER it goes out of scope. But the array proves 4 different scalars. Isn't that a violation of what the my keyword and lexical system does? Whats going on here?C:\Documents and Settings\Owner\Desktop>perl ro.pl $VAR1 = [ \"begin 0 end", \"begin 1 end", \"begin 2 end", \"begin 3 end" ]; C:\Documents and Settings\Owner\Desktop>
|
|---|