<?xml version="1.0" encoding="windows-1252"?>
<node id="941973" title="lexicals are all the same scalar and never go out of scope?" created="2011-12-06 03:39:43" updated="2011-12-06 03:39:43">
<type id="115">
perlquestion</type>
<author id="863652">
patcat88</author>
<data>
<field name="doctext">
&lt;code&gt;
#!/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));    
}
&lt;/code&gt;
readonly on
&lt;code&gt;
C:\Documents and Settings\Owner\Desktop&gt;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&gt;
&lt;/code&gt;
readonly off
&lt;code&gt;
C:\Documents and Settings\Owner\Desktop&gt;perl ro.pl
$VAR1 = [
          \"begin 0 end",
          \"begin 1 end",
          \"begin 2 end",
          \"begin 3 end"
        ];

C:\Documents and Settings\Owner\Desktop&gt;
&lt;/code&gt;
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?</field>
</data>
</node>
