Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: if modifier of a print statement

by jwkrahn (Abbot)
on Jun 30, 2009 at 12:14 UTC ( [id://776003]=note: print w/replies, xml ) Need Help??


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

/no_proc/ ? print "$_\n" : print "$_ : in proc\n" ;

You are using the Conditional Operator in void context.   That is usually written as:

print /no_proc/ ? "$_\n" : "$_ : in proc\n";

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

      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" ; }
Re^3: if modifier of a print statement
by JavaFan (Canon) on Jul 01, 2009 at 09:58 UTC
    You can only write it that way because both clauses in the expression happen to call the same function. If the statement was:
    /no_proc/ ? foo "$_\n" : bar "$_ : in proc\n";
    you could not have applied such a transformation, and the ?: would be left in void context.

    But I don't understand the fear of void context of particular functions or operators. Some people claim to get utterly confused when encountering a map in void context, breaking their tiny little brains on the question whether the author might have intended something else. You don't seem to like ?: in void context. But then you happily use print in void context, totally ignoring its return value.

    Me, I happily use anything in void context if that suits me. And I ain't afraid in the dark either.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-20 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found