Perl's conditional operator in string context -
this question has answer here:
let's take following minimalistic script:
#!/usr/bin/perl # # conditional_operator.pl # use strict; print ( 1 ? "true" : "false" )." statement\n"; exit;
i expect output "true statement". when execute snippet, see ...
deviolog@home:~/test$ perl conditional_operator.pl true
the " statement\n"
concatenation seems ignored.
my perl version v5.14.2. read perlop manual conditional operator , think, string concatenation should possible.
can explain behaviour?
always include use warnings;
@ top of every script.
to desired behavior, add parenthesis print
called entire argument instead of first part:
print(( 1 ? "true" : "false" )." statement\n");
if you'd had warnings
turned on, would've gotten alert:
useless use of concatenation (.) or string in void context
you can avoid undesired behavior leading blank concatenation, or put plus sign before parenthesis:
print +( 1 ? "true" : "false" )." statement\n"; print ''.( 1 ? "true" : "false" )." statement\n";
Comments
Post a Comment