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

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".

Replies are listed 'Best First'.
Re^5: if modifier of a print statement
by Marshall (Canon) on Jun 30, 2009 at 15:58 UTC
    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" ; }