Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

bareword hash keys

by John M. Dlugosz (Monsignor)
on Jun 22, 2003 at 21:49 UTC ( #267992=perlquestion: print w/replies, xml ) Need Help??

John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

What are the rules concerning implicit treatment of barewords as strings when Perl knows it's a hash key? If I didn't know better, I would have thought that the same rules as an identifier apply. But I've seen a leading - character used, such as in named parameters to functions. For example:
require v5.8; use strict; use warnings; my %XX; $XX{a}= 1; $XX{-b}= 2; $XX{+c}= 3; #bareword 'c' not allowed. Interpreted as + applied to f +unction call c(). print (join (", ", keys (%XX)), "\n");
The -b is taken implicitly as '-b', but the same doesn't happen with +c.

Is anything allowed before, among, after the letters besides this? I can't find anything in the perldocs.

—John

Replies are listed 'Best First'.
•Re: bareword hash keys
by merlyn (Sage) on Jun 23, 2003 at 02:38 UTC

      My $h{+__PACKAGE__} is still thanking you for that.

Re: bareword hash keys
by dmmiller2k (Chaplain) on Jun 22, 2003 at 22:15 UTC
    Some modules use hash keys beginning with a '-' for certain attributes of objects defined therein (witness, CGI.pm).

    The use of a leading '+' before an expression within curly braces may be used to force Perl to treat the expression as a hash key or value, rather than parsing the whole thing as a block.

    Not certain if this answers your question precisely, and I am particularly unsure exactly in what ways Perl 5.8 differs from 5.6, but this has been my experience with 5.6 and below.

Re: bareword hash keys
by theorbtwo (Prior) on Jun 23, 2003 at 02:36 UTC

    You confusion is between '-' being part of the bareword, and - as the unary negation operator. perl -MO=Deparse,-p -e "$foo{-bar}" outputs $foo{-'bar'};. The scalar negation operator is defined rather oddly in perl -- when you try to negate a string, it doesn't nummify and then negate (which would give 0), but rather prepends a dash.

    There is still some bareword magic going on, to turn the b (in your example) into 'b' rather then a call to the (non-existant) b() function.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

      Interesting. The "bareword is OK" context must propigate inward, too. I suppose that's a special case of the - and not something that applies to any operator that takes/returns a string. Quick check indicates that's the case.
Re: bareword hash keys
by waswas-fng (Curate) on Jun 22, 2003 at 21:57 UTC
    Why not just get into the habit of using $XX{"+c"} = 3; or $XX{'+c'} = 3;?

    Edited: I know this does not answer your question, but I am really wondering what advantage you get by using barewords that is worth putting special logic in your code to stop people from for instance using a +c as the key.

    -Waswas
      Because it was a royal pain before the bareword-as-key feature! I don't want to go back.
Re: bareword hash keys
by jkenneth (Pilgrim) on Jun 23, 2003 at 14:44 UTC
    Just cause I dealt with it last week, using a . also causes problems as the . gets eaten and doesn't become part of the key.

    Simple example:
    $var{test.dot} = "a"; print keys %var;
    prints testdot.

    I know it's becuase of the dot operator but it still took my by surprise for a second.

    JK
      $ perl use strict; my %var; $var{test.dot} = "a"; ^D Bareword "test" not allowed while "strict subs" in use at - line 3. Bareword "dot" not allowed while "strict subs" in use at - line 3. Execution of - aborted due to compilation errors.
      That's why you should use strict.

      Makeshifts last the longest.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2023-12-10 21:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (41 votes). Check out past polls.

    Notices?