Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

$scalar => 'WTF?';

by {}think (Acolyte)
on Sep 07, 2011 at 00:42 UTC ( #924492=perlquestion: print w/ replies, xml ) Need Help??
{}think has asked for the wisdom of the Perl Monks concerning the following question:

When debuggin a program today, I noticed a simple typo in my scalar assignment. Instead of saying
 my $x = 3;,
I typed
 my $x => 3;
Even with strict and warn, the code ran and executed with no errors, but $x was undefined after the mistyped statement.

I'm very familiar with using the => operator (does it have a name?!?!) for hash initializtion. What exactly happens when on misapplies it to a scalar, as I did? Wouldn't you think this is a good candidate for a warn message?

{}think; #Think outside of the brackets

Comment on $scalar => 'WTF?';
Select or Download Code
Re: $scalar => 'WTF?';
by jwkrahn (Monsignor) on Sep 07, 2011 at 00:49 UTC

    You should compile your code with warnings enabled and the perl would have told you Useless use of a constant in void context because my $x => 3; is the same as my $x, 3;

Re: $scalar => 'WTF?';
by BrowserUk (Pope) on Sep 07, 2011 at 00:49 UTC

    If you enabled warnings, you should have seen:

    Useless use of a constant in void context at ...

    What you coded is exactly the same as:

    my $x, 3;

    Which in turn is equivalent to:

    my $x; 3;

    You declared but did not initialise $x and put a constant in a void context.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      I remember now, that => is simply a syntactical equivalent of the comma operator! This makes sense! Thanks
      {}think; #Think outside of the brackets
Re: $scalar => 'WTF?';
by kejohm (Hermit) on Sep 07, 2011 at 01:21 UTC

    => is known as the 'fat comma operator' and works just like the comma operator, except it causes its left operand to be interpreted as a string if it is a bareword. See comma operator for more info.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (14)
As of 2014-11-27 11:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (183 votes), past polls