in reply to Re^2: if modifier of a print statement
in thread if modifier of a print statement

I think what I have is correct. It compiles and runs under "use strict;" and "use warnings;".

The /no_proc/ is a match statementoperator on $_ which yields a true/false value. If the line in $_ has "no_proc", it gets printed and that's it. If that line doesn't have "no_proc" then the 2nd "else" statement is printed...."$_..in proc".

print will return a "true value". What would "print /adsf/;" mean? I think nothing, but I've been wrong before!

Updated: changed "statement" to "operator"

Replies are listed 'Best First'.
Re^4: if modifier of a print statement
by jwkrahn (Abbot) on Jun 30, 2009 at 13:38 UTC

    I did not say that your code was "incorrect".   What I did say was that  ? : is usually used as an rvalue expression:

    my $variable = /condition/ ? 'TRUE' : 'FALSE';

    or as an lvaue expression:

    ( /condition/ ? $true : $false ) = 'something';

    but very rarely, if ever, used in a void context.

    The /no_proc/ is a match statement on $_ which yields a true/false value.

    The /no_proc/ is a match operator on $_ which yields a true/false value.

    print will return a "true value".

    It will if it worked correctly, otherwise it will return a "false value".

    What would "print /adsf/;" mean? I think nothing,

    "print /adsf/;" would print whatever /adsf/ returned, usually either 1 or '' for "true" or "false".   But that is not what the expression I wrote would do which would print either "$_\n" or "$_ : in proc\n" depending on whether /no_proc/ was "true" or "false".

      I would respectfully disagree on some points and hopefully we can agree on the main point.

      I dunno what got my brain working on the ternary operator idea, maybe it was the "and" or "&&" stuff? (no pun intended). I normally don't use this syntax in Perl code.

      The main point here is that this is a simple "if","else" condition, not a more complex construction with "and" and "next". That's it! That concept is stunning in its simplicity.

      # I think the if/else is better and I think # we agree on that. The if/else can be written like this, but # nobody cares! Let's just stop talking about it! # It simply does NOT matter. We are beating a dead horse if # we worry about this: # /no_proc/ ? print "$_\n" # : print "$_ : in proc\n" ; # # some very few extra parens results in this: # It is crystal clear, there are no "ands, nexts" # that is the MAIN point! # if (/no_proc/) { print "$_\n"; } else { print "$_ : in proc\n" ; }