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

Can you guess the result? Is it a bug?

by BrowserUk (Patriarch)
on Jul 31, 2003 at 02:19 UTC ( #279445=perlquestion: print w/replies, xml ) Need Help??

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

Try and guess the output of the following one-liners before trying them.

perl -wMstrict -e"if( my($test) = 0 ) { print 'Passed'; } else { print + 'Failed'; }" perl -wMstrict -e"if( my($test) = '' ) { print 'Passed'; } else { prin +t 'Failed'; }" perl -wMstrict -e"if( my($test) = undef ) { print 'Passed'; } else { p +rint 'Failed'; }"

Does this consistute a bug?


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Replies are listed 'Best First'.
Re: Can you guess the result? Is it a bug?
by sauoq (Abbot) on Jul 31, 2003 at 02:24 UTC

    They should all pass as they each evaluate to a list with one element.

    Update: And they do. :-) No, not a bug. Remove the parentheses around $test and they will all fail.

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

      To be more precise, each is a list assignment in a scalar (boolean) context and list assignment in a scalar context evaluates to the number of items in the list on the right-hand side of the assignment.

                      - tye
      Okay, but now there's a bit of a discrepancy (not a bug, I guess, but something to wonder about):

      Without the parens around "$test", the first two examples produce a warning:

      Found = in conditional, should be == at -e line 1.
      but the third example ("= undef") does not produce any warning. (I'm running 5.8.0, in case that makes a difference.)

        The warning is just trying to help you out so that you don't have an if that evaluates to a constant boolean value which these all do. Consider:

        ... if($val == 3); #versus ... if($val = 3);

        In the first is checks to see if $val is 3 and in the second it sets $val to be 3 and then checks $val for truth which in this case is always true. So for normal situations that line should just be:

        ...; $val=3;

        Which would have the same effect in my little sample program.

      D'oh. {blush}


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

      Eh? I thought a LIST would evaluate to it's final element in scalar context -- so in this case, they should evaluate to 0, '', and undef respectively.

      Only an ARRAY would eval to the number of elements...

      Am I missing something?

      Matt

        Am I missing something?

        Yes. See tye's reply to me in which he explains it more precisely than I did.

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: Can you guess the result? Is it a bug?
by edoc (Chaplain) on Jul 31, 2003 at 03:28 UTC

    hmmm.. what am I doing wrong here? (perl, v5.8.0 built for i686-linux)

    ]$ perl -wMstrict -e"if( my($test) = 0 ) { print 'Passed'; } else { pr +int 'Failed'; }" Can't declare stub in "my" at -e line 1, near ") =" Execution of -e aborted due to compilation errors. ]$ perl -wMstrict -e"if( my($test) = '' ) { print 'Passed'; } else { p +rint 'Failed'; }" Can't declare stub in "my" at -e line 1, near ") =" Execution of -e aborted due to compilation errors. ]$ perl -wMstrict -e"if( my($test) = undef ) { print 'Passed'; } else +{ print 'Failed'; }" Can't declare stub in "my" at -e line 1, near ") =" Execution of -e aborted due to compilation errors. ]$ perl -wMstrict -e "if( my $test = undef ) { print 'Passed'; } else +{ print 'Failed'; }" syntax error at -e line 1, near "my =" Execution of -e aborted due to compilation errors.

    I had to swap "/' to get it to run..

    ]$ perl -wMstrict -e 'if( my ($test) = undef ) { print "Passed"; } els +e { print "Failed"; }' Passed ]$ perl -wMstrict -e 'if( my $test = undef ) { print "Passed"; } else +{ print "Failed"; }' Failed

    cheers,

    J

      Because $test is in double-quotes, your shell is trying to interpolate it. Since it isn't set though (and there aren't such things as warnings and strict for the shell ; ), it is replaced with nothing and becomes:
      perl -wMstrict -e"if( my() = 0 ) { print 'Passed'; } else { print 'Fai +led'; }"
      If you escape the $ you'll be fine.
      perl -wMstrict -e"if( my(\$test) = 0 ) { print 'Passed'; } else { prin +t 'Failed'; }"
        A better way is to make sure the shell doesnt even try...
        perl -wMstrict -e'if( my() = 0 ) { print "Passed"; } else { print "Failed"; }'

        T I M T O W T D I

      Different command line semantics between Win32 and linux shells.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2022-09-26 10:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (117 votes). Check out past polls.

    Notices?