in reply to How should named paramater keys be named?

maybe in part because the fat comma does work its quotish magic for hyphenated parameter names:

use strict; use warnings; params (-first => 'first', -second => 'second'); sub params { my %params = @_; print "$_: $params{$_}\n" for sort keys %params; }

Prints:

-first: first -second: second

and thus disambiguates somewhat between parameter names and values? The magic only works for a leading -, anywhere else it turns into a subtraction operator with less desirable results!

True laziness is hard work

Replies are listed 'Best First'.
Re^2: How should named paramater keys be named?
by davido (Cardinal) on Jun 28, 2011 at 02:03 UTC

    Should that behavior be considered to be at odds with perlop? The POD enumerates what would be considered fat-comma-quotable.

    Maybe it's an example of the notion that perl is the ultimate authority on Perl.


    Dave

      I was worried about even posting the reply because I haven't a good story for why it should work apart from Perl DWIMery. As you point out it's not documented as providing that behaviour in any obvious place.

      On the other hand it would break a huge amount of code should that behaviour change so I'd guess it ought be safe enough to use.

      True laziness is hard work

        perlmodstyle mentions the use of named parameters with a leading hyphen.

      I don't think its related to fat-comma, observe
      $ perl -le print-localtime
      -Tue Jun 28 00:16:25 2011
      

        Oh!!! Thank you! Well, now you led me to remember an answer, while opening a question.

        When you mentioned that it's not related to the fat-comma, you reminded me that hyphen itself is special, and that led me to this in perlop Symbolic Unary Operators:

        Unary "-" performs arithmetic negation if the operand is numeric, including any string that looks like a number. If the operand is an identifier, a string consisting of a minus sign concatenated with the identifier is returned. Otherwise, if the string starts with a plus or minus, a string starting with the opposite sign is returned. One effect of these rules is that -bareword is equivalent to the string "-bareword". If, however, the string begins with a non-alphabetic character (excluding "+" or "-"), Perl will attempt to convert the string to a numeric and the arithmetic negation is performed. If the string cannot be cleanly converted to a numeric, Perl will give the warning Argument "the string" isn't numeric in negation (-) at ....

        So that answers that: It's not fat-comma that's quoting a -key=>'value' pair, it's the hyphen.

        And that leads to the question. Why is -localhost not treated like -bareword, or in other words, "-localhost"?


        Dave