in reply to if modifier of a print statement

Yes, this is an op precedence problem. But why even confront that?
my @test = qw(no_proc line_to_proc1 line_to_proc2); foreach (@test) { # this is actually just one line, indented to # make the if/else more clear. The trailing ";" # isn't even necessary. # this looks like a simple "we do either a or b, # but we will do one of them for sure situation, # ie if/else. The single statement below probably # equates to exactly the same machine code as a # more lengthy if/else statement. There is no # "next", there is no "and". /no_proc/ ? print "$_\n" : print "$_ : in proc\n" ; } __END__ prints: no_proc line_to_proc1 : in proc line_to_proc2 : in proc

Replies are listed 'Best First'.
Re^2: if modifier of a print statement
by jwkrahn (Abbot) on Jun 30, 2009 at 12:14 UTC
    /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";
      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".

      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.