in reply to Re: Re: Using @- & @+
in thread Using @- & @+
#use strict; #uncomment to kill the code my $s = 'foo'; $$s = 'bar'; print "\$foo = $foo\n"; $foo = 'baz; print "\$\$s = $$s\n"; __END__ Output: $foo = bar $$s = baz
$$s is $foo in this case - using a variable for a variable name. There's a huge rant here by Mark-Jason Dominus (at the bottom, there are links to part 2 and part 3) about symbolic references and why they are bad. It's a good read. I love it when he says "Yeah, well, there's no problem with smoking in bed, either, as long as you don't fall asleep and burn the house down."
That said, symrefs have their place in perl. If you know you want symrefs, put no strict 'refs'; in the smallest possible scope and they will work inside of that scope. In your case, try:
if ($#-) { no strict 'refs'; print "$#+ matches found", $/; for (1..$#+) { print "\$_ is $_, & \$1 is $1", $/; print "$haystack contains ${$_} at $-[$_]", $/; } }
${$_} will now expand to $1 and print the value of $1.
|
|---|