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

Multiple commands separated by commas

by tel2 (Scribe)
on Sep 01, 2011 at 05:51 UTC ( #923566=perlquestion: print w/ replies, xml ) Need Help??
tel2 has asked for the wisdom of the Perl Monks concerning the following question:

Dearly beloved Monks,

We are gathered here today to try to help me understand what kinds of commands can be strung together with commas, and what kinds can't, for concise coding.

For example, this works as I would expect (i.e. $x gets the value of $i before the "next" command occurs:

perl -e 'for $i (1..9){$x = $i, next if $i == 5;print $i};END{print "\ +n$x\n"}' Output: 12346789 5

But here, I would have expected "Next" to print, but as you can see, it doesn't:

perl -e 'for $i (1..9){print "Next", next if $i == 5;print $i}' Output: 12346789
Questions:
1. Why does '$x = $i' get executed, but 'print "Next"' seems not to, in the above examples?
2. Why didn't I get some kind of error/warning (still nothing even with the '-w' switch), in my 2nd example?
3. Where can I read up about which kinds of commands can be strung together with commas, and which can't?

Thank you!
tel2

Comment on Multiple commands separated by commas
Select or Download Code
Re: Multiple commands separated by commas
by BrowserUk (Pope) on Sep 01, 2011 at 06:14 UTC

    Try it this way:

    perl -e "for $i (1..9){print( 'Next' ), next if $i == 5;print $i}" 1234Next6789

    Note the parens around the print arg, and remember to switch the quotes around for *nix.


    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.
Re: Multiple commands separated by commas
by JavaFan (Canon) on Sep 01, 2011 at 06:24 UTC
    1. Precedence. The first is parsed as (($x = $i), next), the latter as (print("Next", next)). In the latter, next is evaluated before the print.
    2. Why? Do not assume Perl is made to be used by idiots, with warnings and flashing lights at any corner. Warning are more "uhm, hey, you know, this construct you are using, it may not do what you expect it to do". That's not the case in your second example. It can only mean one thing. But feel free to submit a patch that makes this warnable. (Remember: the golden rule of "why isn't X in Perl?" is "because you haven't written a patch to add it yet")
    3. Any two expressions can be separated by commas. See the perlop manual page.
Re: Multiple commands separated by commas
by tel2 (Scribe) on Sep 01, 2011 at 22:43 UTC

    Thanks guys.

    Much appreciated.

    tel2

Re: Multiple commands separated by commas
by Marshall (Prior) on Sep 03, 2011 at 04:38 UTC
    The comma operator can be complicated.
    I would not use the comma operator in this complex of a situation.

    If there are multiple parts, put () around the comma parts(eg (part1), (part2,)) and it will parse like you expect.

    #!usr/bin/perl -w use strict; for my $i (1..9) { (print "Next"), next if $i == 5; print $i } __END__ Output: 1234Next6789
    Another what I think is better coding (don't use the comma if not needed):
    #!usr/bin/perl -w use strict; for my $i (1..9) { if ($i == 5) { print "Next"; next; } print $i; } __END__ Output: 1234Next6789
    The basic thing with the comma operator, is that what appears in the last part of the comma statement is what matters.
Re: Multiple commands separated by commas
by tel2 (Scribe) on Sep 08, 2011 at 00:12 UTC
    Thanks Marshall,

    Sorry for the delay in responding - just noticed your post today.

    So it seems that I can use:
    print ("Next"), next if $i == 5;
    OR:
    (print "Next"), next if $i == 5;

    > Another what I think is better coding (don't use the comma if not needed):
    Yes, I wouldn't use it if I was splitting the commands over multiple lines (as in your 1st code example), but I like to do such things in one line for conciseness, sometimes, e.g.:
    $a = $b ? $c : $d;
    instead of:
    if ($b) { $a = $c; } else { $a = $d; }
    > The basic thing with the comma operator, is that what appears in the last part of the comma statement is what matters.
    What do you mean, exactly, Marshall?  An example may help me.

    Thanks. tel2

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (16)
As of 2014-08-22 14:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (158 votes), past polls