Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Strange interaction between print and the ternary conditional operator

by davido (Cardinal)
on Feb 20, 2020 at 16:13 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11113255]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2024-04-18 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found