Any set of braces that isn't part of another expression (such as an if-block or a variable usage) are interpreted as
either a hash ref
or block of code. Perl uses heuristics to figure out which. If you find that Perl is guessing wrong, you can make it look more like what you want it to be.
Generally speaking, an empty set is going to be treated as a hashref. If, for some unfathomable reason, you want a no-op block, stick a semicolon inside it:
perl -we "use strict; {;} no strict 'refs';"
Perl sees that you've got code (an empty statement) in there, so it must be a code block. No warning about
Useless use of single ref constructor like you'd get if you used
{}; instead. (I turned on warnings so you could see a difference.)
Conversely, if you want to make Perl interpret what looks like a block of code to be a hashref, put it in parentheses or stick a plus on the front. (I personally dislike the + convention.)
F:\Perl>perl -we "use strict; {qw(key val)}; no strict 'refs';"
Useless use of a constant in void context at -e line 1.
Useless use of a constant in void context at -e line 1.
F:\Perl>perl -we "use strict; ({qw(key val)}); no strict 'refs';"
Useless use of single ref constructor in void context at -e line 1
The second example is half as useless.
:-)
Caution: Contents may have been coded under pressure.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.