Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Unquoted strings with strict

by robin (Chaplain)
on Dec 12, 2001 at 23:33 UTC ( [id://131361]=perlquestion: print w/replies, xml ) Need Help??

robin has asked for the wisdom of the Perl Monks concerning the following question:

You can assign an unquoted string to a variable, even when strict mode is on, like this:
use warnings; use strict; my $foo = hello =>; print $foo;
(This is more of an obfuscation than a request for enlightenment, but the Obfuscated code section seems to be reserved for complete obfuscated programs. Sorry if this is in the wrong place.)

Replies are listed 'Best First'.
Re: Unquoted strings with strict
by premchai21 (Curate) on Dec 12, 2001 at 23:41 UTC

    This is documented, and used most often in hashes, like this:

    %hash = ( foo => 'bar', # note 'foo' not needed bar => 'baz', baz => 'foo', items => 3 ); print $hash{items}; # note 'items' not needed

    The => acts like a special sort of comma, which quotes barewords on the left; barewords are also quoted in hash lookups.

Re: Unquoted strings with strict
by Vavoom (Scribe) on Dec 12, 2001 at 23:45 UTC
    Technically, => quotes its left side argument, making this:
    my $foo = hello =>;
    equivalent to this:
    my $foo = 'hello',;

    Vavoom
Re: Unquoted strings with strict
by kwoff (Friar) on Dec 12, 2001 at 23:38 UTC
    I think it is a quoted string, though, because of the => operator. Maybe it's only a matter of semantics, though.
      It's arguable, I suppose. Maybe it's better to use => consistently:
      use warnings; use strict; ;my $foo => ;$foo = hello => ;print $foo =>
      (updated for symmetry)
Re: Unquoted strings with strict
by tadman (Prior) on Dec 12, 2001 at 23:43 UTC
    This seems to be some sort of magical property of the arrow operator. Any alphanumeric+underscore type text before it is recognized as an unquoted string, and this even applies to text inside curly braces, such as:
    foo => $x same as: 'foo' => $x $x{foo} same as: $x{'foo'}

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found