Using ${$_} this way is called a "symbolic reference" or a "soft reference". use strict doesn't allow the use of symrefs. For example:

#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.


In reply to Re: Re: Re: Using @- & @+ by jsprat
in thread Using @- & @+ by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.