You don't need to be an utter cowdawg towards the OP -- strict and warnings are irrelevant to his issue.
I don't fully grok the semantics of foreach but I suspect the correct answer is as follows:
$sref = \$string sets $sref to point to the variable $string. All is well here. But then foreach enters the picture and localises $string -- this $string is only available to the inner loop. $sref continues pointing to the $string in the outer loop. Testing this without the string reference:
my $string = "I'm still here"; say $string; my @array_of_strings = qw/foo bar baz quux/; foreach $string ( @array_of_strings ) { say $string; } say $string; # output I'm still here foo bar baz quux I'm still here
Yep, looks like what I described. Now, if you add the $sref, you'll notice it'll print the outer loop value for the whole time -- it points to the wrong $string.
In reply to Re^2: Dereference string in foreach
by Anonymous Monk
in thread Dereference string in foreach
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |