Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Strange interaction between print and the ternary conditional operator

by davido (Cardinal)
on Feb 20, 2020 at 16:13 UTC ( #11113255=note: print w/replies, xml ) Need Help??


in reply to Strange interaction between print and the ternary conditional operator

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2020-05-26 18:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (150 votes). Check out past polls.

    Notices?