Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Strange interaction between print and the ternary conditional operator

by WingedKnight (Initiate)
on Feb 20, 2020 at 03:17 UTC ( #11113223=perlquestion: print w/replies, xml ) Need Help??

WingedKnight has asked for the wisdom of the Perl Monks concerning the following question:

Ran into a strange interaction between print and the ternary conditional operator that I don't understand. If we do...:

print 'foo, ' . (1 ? 'yes' : 'no') . ' bar';

...then we get the output...:

foo, yes bar

...as we would expect. However, if we do...:

print (1 ? 'yes' : 'no') . ' bar';

...then we just get the output...:

yes

Why isn't " bar" getting appended to the output in the second case?

Replies are listed 'Best First'.
Re: Strange interaction between print and the ternary conditional operator
by kcott (Bishop) on Feb 20, 2020 at 07:22 UTC

    G'day WingedKnight,

    Welcome to the Monastery.

    When in doubt, it's often a good idea to find out how Perl is seeing your code with B::Deparse.

    $ perl -MO=Deparse,-p -e 'print "foo, " . (1 ? "yes" : "no") . " bar"; +' print('foo, yes bar'); -e syntax OK
    $ perl -MO=Deparse,-p -e 'print (1 ? "yes" : "no") . " bar";' (print('yes') . ' bar'); -e syntax OK
    "Why isn't " bar" getting appended to the output in the second case?"

    Because it's getting appended to the return value from print.

    $ perl -e '$x = print "foo, " . (1 ? "yes" : "no") . " bar"; print "\n +$x\n"' foo, yes bar 1
    $ perl -e '$x = print (1 ? "yes" : "no") . " bar"; print "\n$x\n"' yes 1 bar

    Of course, you should start all your code with:

    use strict; use warnings;

    In this case, you would have received warnings:

    $ perl -e 'use strict; use warnings; print (1 ? "yes" : "no") . " bar" +;' print (...) interpreted as function at -e line 1. Useless use of concatenation (.) or string in void context at -e line +1. yes

    See "perlintro: Safety net".

    — Ken

Re: Strange interaction between print and the ternary conditional operator
by tybalt89 (Parson) on Feb 20, 2020 at 03:39 UTC

    Try

    print +(1 ? 'yes' : 'no') . ' bar';

    And then read the top of perlfunc

Re: Strange interaction between print and the ternary conditional operator
by jwkrahn (Monsignor) on Feb 20, 2020 at 07:20 UTC

    Also, try it with commas instead of concatenation and you won't need parentheses and you won't have this problem:

    print 'foo, ', 1 ? 'yes' : 'no', ' bar';
    print 1 ? 'yes' : 'no', ' bar';
Re: Strange interaction between print and the ternary conditional operator
by davido (Cardinal) on Feb 20, 2020 at 16:13 UTC
    my $call_resp = print(1 ? 'yes' : 'no) . 'bar'; # yes print "\nprint() function returned: $call_resp\n"; # \nprint() funct +ion returned: 1bar\n

    print returns a true value on success, and a false value if it failed. The parenthesis, the way you're using them, become the arg list for print. So you're asking Perl to print 'yes', and then to append 'bar' to the return response from print. This is almost equivalent, and may help explain what's happening:

    (print 1 ? 'yes' : 'no') . 'bar';

    A common disambiguation is +(...). I wasn't sure it would be appropriate when concatenating later, but I just verified that it works fine:

    print +(1 ? 'yes' : 'no') . "bar\n"; # yesbar\n

    Dave

Re: Strange interaction between print and the ternary conditional operator
by Anonymous Monk on Feb 20, 2020 at 03:27 UTC
    print works like a function, so print $foo acts like print($foo). you gotta print something before the parens to make it work, like print "".(1?"y":"n")."x" or better yet use join print join "", (1?"y":"n"), "x"
Re: Strange interaction between print and the ternary conditional operator
by WingedKnight (Initiate) on Feb 21, 2020 at 07:02 UTC
    I get it now. Thank you guys for the answers/replies. :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2020-04-07 01:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (42 votes). Check out past polls.

    Notices?