Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

What does the dash before hash assignment means?

by anaconda_wly (Scribe)
on Sep 03, 2013 at 09:06 UTC ( #1052045=perlquestion: print w/ replies, xml ) Need Help??
anaconda_wly has asked for the wisdom of the Perl Monks concerning the following question:

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);

Comment on What does the dash before hash assignment means?
Download Code
Re: What does the dash before hash assignment means?
by mtmcc (Hermit) on Sep 03, 2013 at 09:11 UTC
    Have a look at this, and search for 'hyphen'.
Re: What does the dash before hash assignment mean?
by Athanasius (Monsignor) on Sep 03, 2013 at 09:12 UTC

    No special meaning; a hash key is a string, and the dash is merely part of the string:

    19:07 >perl -MData::Dump -wE "my %h = (-url => 'abcd'); dd %h;" ("-url", "abcd") 19:08 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: What does the dash before hash assignment means?
by Anonymous Monk on Sep 03, 2013 at 09:27 UTC
Re: What does the dash before hash assignment means?
by hdb (Prior) on Sep 03, 2013 at 09:28 UTC

    The => is just a grandified comma interpreting its left side as a string, so in your question it is the same as fun( '-url', $httpRefer ). So no hash involved yet unless the called function creates one. See these two examples:

    use strict; use warnings; use Data::Dumper; sub fun1 { print Dumper \@_; } sub fun2 { my %args = @_; print Dumper \%args } my $httpRefer = 'someValue'; fun1( -url => $httpRefer ); fun2( -url => $httpRefer ); __END__ $VAR1 = [ '-url', 'someValue' ]; $VAR1 = { '-url' => 'someValue' };

      The fat comma is a bit of a red herring. The same is true with a regular comma...

      fun1( -url, $httpRefer ); fun2( -url, $httpRefer );
      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

        Interesting, thanks for this comment! It works in the same way, unless one omits the hyphen. Without it one gets an Bareword "url" not allowed while "strict subs" in use at dash.pl line 9. error.



Re: What does the dash before hash assignment means?
by sundialsvc4 (Abbot) on Sep 03, 2013 at 12:13 UTC

    fun(-url => $httpRefer ); is not a “hash assignment.”   The => symbol in this case is equivalent to a comma.   Subroutine fun is being called with an argument-list containing two arguments:   -url, and the value of $httpRefer.

    Some subroutines are written to recognize and to assign particular meanings to parameters which begin with a dash, e.g. to disambiguate user-chosen arbitrary strings which otherwise might match “real” parameters for the function.   (I, the author, decree that ... “Anything beginning with a dash is a literal.”)   But, that is up to their authors, not Perl itself.

      "...is not a hash assignment"

      It is.

      #!/usr/bin/env perl + use strict; use warnings; print fun(-nose =>'cuke') . qq(\n); sub fun { my %args = @_; return $args{-nose}; } __END__ karls-mac-mini:monks karl$ ./fun.pl cuke

      «The Crux of the Biscuit is the Apostrophe»

        Not really - the arguments to the function call construct a list:

        -nose =>'cuke' # even-sized list

        The hash assignment happens here:

        my %args = @_;
Re: What does the dash before hash assignment means?
by kcott (Abbot) on Sep 03, 2013 at 21:02 UTC

    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

      thanks everyone! I see.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1052045]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-12-19 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (69 votes), past polls