in reply to Re: Dereference string in foreach
in thread Dereference string in foreach
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Dereference string in foreach
by Anonymous Monk on Feb 13, 2013 at 21:32 UTC | |
Re^3: Dereference string in foreach
by blue_cowdawg (Monsignor) on Feb 14, 2013 at 15:30 UTC | |
by LanX (Saint) on Feb 14, 2013 at 22:12 UTC | |
by Anonymous Monk on Feb 14, 2013 at 19:29 UTC | |
by Anonymous Monk on Feb 14, 2013 at 21:34 UTC |
In Section
Seekers of Perl Wisdom