Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Why is this auto-quoted?

by nbtrap (Sexton)
on Jul 30, 2013 at 21:39 UTC ( #1047125=perlquestion: print w/ replies, xml ) Need Help??
nbtrap has asked for the wisdom of the Perl Monks concerning the following question:

There are two cases where I can't figure out why bare identifiers are auto-quoted, even under strictures:
use strict; my %hash = (-foo => 'bar');
perlop(1) says that "The "=>" operator is a synonym for the comma except that it causes a word on its left to be interpreted as a string if it begins with a letter or underscore and is composed only of letters, digits and underscores." Why is "-foo" quoted?

Now, the second example:

use strict; use Gtk2 -init;
Why does this work? If I use just "init" without the dash, perl complains of a bareword.

Comment on Why is this auto-quoted?
Select or Download Code
Re: Why is this auto-quoted? (because)
by Anonymous Monk on Jul 30, 2013 at 22:13 UTC

    ... why ...

    Because, because it is, see http://perldoc.perl.org/perlop.html#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

    See also Modern Perl page 170 Barewords

Re: Why is this auto-quoted?
by tobyink (Abbot) on Jul 30, 2013 at 22:19 UTC

    This is really only one case. In the example with the "fat comma" operator (=>), the fat comma isn't doing any quoting. The -foo gets is quoted anyway; without the fat comma...

    my %hash = (-foo, 'bar');

    The weird -foo thing is documented in perlop:

    Unary "-" performs arithmetic negation if the operand is numeric. 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.

    So it's a documented feature of Perl. Why was the feature added? I'm not quite sure of the history. It's possible that it was originally a mistake that arose in the code that parsed file test operators (see -X in perlfunc), and then became too widely used to change. Or maybe it was intentional to begin with.

    Either way, it's quite a cute way to accept option names to functions, etc without needing to quote them.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      That makes sense. The reason I missed it in the documentation was that I was looking at the documentation of => and not -.

      Why was the feature added? I'm not quite sure of the history.

      Tcl/Tk likes it

      grid [ttk::button .b -text "Hello World"]

      One effect of these rules is that -bareword is equivalent to the string "-bareword"
      Not really quit!. Check with B::Deparse, then you will discovery that -bareword is equalvalent to -'bareword' not '-bareword'

      perl -MO=Deparse -e' use warnings; use strict; my %ha = ( -foo => "bar"); print %ha; '
      gives
      use warnings; use strict; my %ha = (-'foo', 'bar'); print %ha; -e syntax OK
      Though with "-foo" there is no need of "=>".

        Not really quit!

        You know what the difference between -'foo' and '-foo' is? nothing

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1047125]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2014-09-21 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls