When curly braces follow a sigil ('@', '%' or '$'), the expression inside the braces is evaluated as a string, which is then treated as the name of a variable to be accessed according to the given sigil. I'm basing that on observed behavior -- I haven't found the particular manual that lays this out in detail; note the following one liner (which crucially does not 'use strict'):
perl -le '%x=(y=>"a"); $a="Z";print ${$x{y}}'
It creates a hash (%x) containing a single key/value pair ("y"/"a"); it also assigns a value of "Z" to variable $a (which happens to be a predefined global variable in perl). Then it uses the hash to look up the string "a", attaches the "$" sigil to that, and returns the value assigned to $a.

If 'use strict' were in effect for the one-liner above, it would die with the error: "Can't use string ("a") as a SCALAR ref while "strict refs" in use at -e line 1." Of course, there are cases where curlies are useful, even with strictures in effect, for referring to chunks of data structures -- e.g.:

use strict; my %hoa = ( foo => [0, 1], bar => [2, 3] ); for ( keys %hoa ) { print " $_ contains: @{$hoa{$_}}\n"; }

It's not that you "can't say" something like @${a} (you can, provided you don't 'use strict') -- it's just that this doesn't do what you thought it would do. (What did you think it would do?) What it actually does is treat the predefined global variable "$a" as a reference to an array, and tries to dereference it so as to return the array. If 'strict refs' is in effect, this is an illegal operation; in the absence of 'strict refs', if $a was not assigned an array reference as its value, you'll just get (undefined) instead of an array.


In reply to Re: syntax issue by graff
in thread syntax issue by sueme

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.