Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Detecting 'our $foo => 1' mistake? (updated)

by haukex (Archbishop)
on Jan 21, 2017 at 10:34 UTC ( [id://1180078]=note: print w/replies, xml ) Need Help??


in reply to Detecting 'our $foo => 1' mistake?

Hi perlancar,

Could you provide an SSCCE that demonstrates the problem? As the AM already pointed out, the code you showed does provide a warning:

$ perl -wMstrict -e 'our $foo => { a=>"blah", b=>"blah" };' Useless use of anonymous hash ({}) in void context at -e line 1.

Update: Ok, the code you showed in the title does not warn. Standard Perl::Critic doesn't seem to catch this one either, and I don't yet see any add-on policies that catch this. Update 2: See my other reply.

$ perl -wMstrict -e 'our $foo => 1;' $

Regards,
-- Hauke D

Replies are listed 'Best First'.
Re^2: Detecting 'our $foo => 1' mistake? (scalar comma evaluating to 1)
by LanX (Saint) on Jan 21, 2017 at 11:04 UTC
    Without being able to test I think the parser sees a list in void context, which is reduced to the last statement.

    The => is just a fat comma and the declaration resets the var to undef.

    So our $foo => 1; -> undef, 1;

    I suppose that at runtime the scalar comma operator will only try to "return" the last statement to void (I know this sounds weird°)

    And 0 and 1 are allowed in void (think modules needing a true return value).

    That's why the error is only caught for other values.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

    °) from Programming Perl

    Binary "," is the comma operator. In scalar context it evaluates its left argument in void context, throws that value away, then evaluates its right argument in scalar context and returns that value.

      Hi Rolf,

      Yes, I think you're right:

      $ perl -MO=Deparse,-p -e 'our $foo => 1;' (our($foo), '???');

      Regards,
      -- Hauke D

        Yeah and I checked, it's a pure runtime error.

        I.e. perl -c won't catch.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Re^2: Detecting 'our $foo => 1' mistake? (updated)
by ikegami (Patriarch) on Jan 23, 2017 at 17:14 UTC

    0 and 1 in void context don't warn so you can do stuff like 1 while s/.../.../;.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-19 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found