Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: "last expression" quiz

by fxn (Sexton)
on Oct 19, 2005 at 22:21 UTC ( [id://501455]=note: print w/replies, xml ) Need Help??


in reply to Re: "last expression" quiz
in thread "last expression" quiz

Oh, yes, AND not issue a warning:

% perl -wle 'print 1 if "" == 0' Argument "" isn't numeric in numeric eq (==) at -e line 1. 1

Replies are listed 'Best First'.
Re^3: "last expression" quiz
by bart (Canon) on Oct 19, 2005 at 22:27 UTC
    A for loop typically returns nothing, whatever that means...

    Judging by your results on the warnings, or rather the lack of them, I think the subs return false, thus, !1. That is both equal to "" and numerically equal to 0 without a warning.

    Oh, and a 1 is allowed to be used as a statement without warning. Typical use for them is in a bodyless loop:

    1 while COND;
    as well as as the last statement in a required file/module. Hence that there's no warning for a bare 1;.

      Actually, a for loop does return something. Assuming it runs at least once, it will return an empty string, alias scalar false.

      There seems to be no particular reason for that behaviour.

      Makeshifts last the longest.

        Actually, bart is saying it returns !1 which is really scalar false. It is subtly different than "" as can be seen here:

        $ perl -wle 'print "yes" if !1 == 0 ' yes $ perl -wle 'print "yes" if "" == 0 ' Argument "" isn't numeric in numeric eq (==) at -e line 1. yes

        -sauoq
        "My two cents aren't worth a dime.";
        

      Oh, and another thing: you can use either 0 or 1 as a constant in void context without getting a warning, but you can also use any string that starts with "ig", "ds", or "di", as Dominus famously demonstrated.

      Makeshifts last the longest.

      as well as as the last statement in a required file/module.

      Well, you can use whatever you want as the last statement of a required module without getting the "Useless use of a constant in void context" warning.

      -sauoq
      "My two cents aren't worth a dime.";
      
        True, if you actually require the file. That probably is because then, the last statement isn't called in void context.

        But you do get the warning if you try to test such a module for syntax errors using perl -wc FILE. Unless you actually use 1.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-26 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found