G'day anaconda_wly,

"I saw some perl code with a dash before key name for hash assignment. Any special meaning? Does that equal to a quotation mark? Thanks for any help. fun(-url => $httpRefer);"

Firstly, let's look at how Perl parses that (with and without the dash):

$ perl -MO=Deparse,-p -e 'fun(-url => $httpRefer);' fun((-'url'), $httpRefer); -e syntax OK $ perl -MO=Deparse,-p -e 'fun(url => $httpRefer);' fun('url', $httpRefer); -e syntax OK

So, clearly something special is happening. Is it the dash or the fat comma?

$ perl -MO=Deparse,-p -e 'fun(-url);' fun((-'url')); -e syntax OK

No fat comma and we're still getting "-url" parsed as "(-'url')". This indicates that the dash is the cause; the perlop: Symbolic Unary Operators documentation bears this out:

"If the operand is an identifier, a string consisting of a minus sign concatenated with the identifier is returned."

Without actually looking at the parsing process, you could have run some simple tests:

$ perl -Mstrict -Mwarnings -le ' sub fun { print "@_" } fun(1, url); fun(2, -url); fun(3, -url, "httpRefer"); fun(4, -url => "httpRefer"); fun(5, url, "httpRefer"); fun(6, url => "httpRefer"); ' Bareword "url" not allowed while "strict subs" in use at -e line 3. Bareword "url"¬ allowed while "strict subs" in use at -e line 7. Execution of -e aborted due to compilation errors.

Commenting out the lines with barewords:

$ perl -Mstrict -Mwarnings -le ' sub fun { print "@_" } #fun(1, url); fun(2, -url); fun(3, -url, "httpRefer"); fun(4, -url => "httpRefer"); #fun(5, url, "httpRefer"); fun(6, url => "httpRefer"); ' 2 -url 3 -url httpRefer 4 -url httpRefer 6 url httpRefer

While I can see that "(-url => $httpRefer)" might be used as a key/value pair in some hash assignment, you don't actually show any code performing such an operation. [If you thought you did, you may need to ask another question :-)] Regardless, the code I've posted here involves no hashes at all: the unary minus operator, in this context, is unrelated to hashes!

-- Ken


In reply to Re: What does the dash before hash assignment means? by kcott
in thread What does the dash before hash assignment means? by anaconda_wly

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.