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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: strange behaviour in perl ..please explain
by moritz (Cardinal) on Jan 23, 2009 at 10:11 UTC
    Your question is very hard to read, and I see no reason why it should be all bold....

    print "no" is an expression which returns 1 (print successful), and which prints no as a side effect. The (outer) print than has "yes", 1 as the argument list, so it prints yes1

    The second example can be explained along the same lines: exit is evaluated before the print, and since it quits the program, the print isn't evaluated at all.

Re: strange behaviour in perl ..please explain
by Anonymous Monk on Jan 23, 2009 at 10:10 UTC
    You should know how to format your posts, read Markup in the Monastery.
    D:\>cat bob $var=1; print"yes",print"no" if ($var) D:\>perl -MO=Deparse,-p bob ($var = 1); ($var and print('yes', print('no'))); bob syntax OK
    B::Deparse
Re: strange behaviour in perl ..please explain
by ikegami (Patriarch) on Jan 23, 2009 at 15:41 UTC

    You have problems understanding where your argument lists end. It would help you to use parens around your arguments.

    print"yes",print"no" if ($var);
    means
    print( "yes", print("no") ) if ($var);
    1. The inner print outputs no.
    2. The outer print outputs yes and the return value of the inner print (1 for success).

    print"yes",exit ,print"no" if ($var);
    means
    print( "yes", exit(), print("no") ) if ($var);
    Arguments are evaluated from left to right, so
    1. "yes" is placed on the stack
    2. exit is evaluated and causes perl to exit.
    3. The inner print never gets to execute.
    4. The outer print never gets to execute.

    print"yes";exit ,print"no" if ($var);
    means
    print"yes";
    exit(), print("no") if ($var);
    Line breaks and the lack thereof are not significant to Perl, so
    1. print("yes") is executed unconditionally.
    2. exit is evaluated and causes perl to exit.
    3. print("no") never gets to execute.

    print"yes";print"no",exit if ($var);
    means
    print"yes";
    print( "no", exit() ) if ($var);
    1. print("yes") is executed unconditionally.
    2. "no" is placed on the stack
    3. exit is evaluated and causes perl to exit.
    4. The second print never gets to execute.

Re: strange behaviour in perl ..please explain
by puudeli (Pilgrim) on Jan 23, 2009 at 10:16 UTC

    1. print can take a list as an argument, thus the return value (1 ie. true) of the print"no" gets printed (comma is a list separator).
    2. exit gets evaluated and your script exits.
    3. The last part of your statement is evaluated first, that means that if( $var ) exit is executed first and it exits.

    Update: corrected #2, thanks ikegami

    --
    seek $her, $from, $everywhere if exists $true{love};

      exit evaluates given expression, your print statement in this case and exits.

      No. exit is evaluated with no arguments in all of the OP's snippets.

      print"yes",exit ,print"no" if ($var)

      is the same as

      print( "yes", exit(), print("no") ) if ($var)

      That's because

      print( "yes", exit( , print("no") ) ) if ($var)

      is illegal.

        Ah, I stand corrected, I missed the comma :-)

        --
        seek $her, $from, $everywhere if exists $true{love};
Re: strange behaviour in perl ..please explain
by poolpi (Hermit) on Jan 23, 2009 at 10:13 UTC

    Try it with these pragmas :

    use strict; use warnings;

    Update:
    You may have a look at the ternary operator in perlop

    my $var = 1; print ($var ? 'yes' : 'no');


    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb