Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl's conditional operator in string context [duplicate]

Tags:

perl

Let's take the following minimalistic script:

#!/usr/bin/perl

#
# conditional_operator.pl
#

use strict;

print ( 1 ? "true" : "false" )." statement\n";

exit;

I expect the output always to be "true statement". But when I execute this snippet, I see ...

deviolog@home:~/test$ perl conditional_operator.pl
true

The " statement\n" concatenation seems to be ignored.

My perl version is v5.14.2. I read the perlop manual about the conditional operator and think, a string concatenation should be possible.

Can somebody explain this behaviour?


2 Answers

Always include use warnings; at the top of every script.

To get your desired behavior, just add parenthesis so print is called with the entire argument instead of just the first part:

print(( 1 ? "true" : "false" )." statement\n");

If you'd had warnings turned on, you would've gotten this alert:

Useless use of concatenation (.) or string in void context

You can also avoid the undesired behavior by leading with a blank concatenation, or you could put a plus sign before the parenthesis:

print +( 1 ? "true" : "false" )." statement\n";
print ''.( 1 ? "true" : "false" )." statement\n";
like image 154
Miller Avatar answered Apr 02 '26 12:04

Miller


Adding use warnings to your code gives this:

print (...) interpreted as function at ./cond line 10.

Useless use of concatenation (.) or string in void context at ./cond line 10.

Even better, add use diagnostics and you get this:

print (...) interpreted as function at ./cond line 11 (#1)

(W syntax) You've run afoul of the rule that says that any list operator followed by parentheses turns into a function, with all the list operators arguments found inside the parentheses. See "Terms and List Operators (Leftward)" in perlop.

Useless use of concatenation (.) or string in void context at ./cond line 11 (#2)

(W void) You did something without a side effect in a context that does nothing with the return value, such as a statement that doesn't return a value from a block, or the left side of a scalar comma operator. Very often this points not to stupidity on your part, but a failure of Perl to parse your program the way you thought it would. For example, you'd get this if you mixed up your C precedence with Python precedence and said

$one, $two = 1, 2;

when you meant to say

($one, $two) = (1, 2);

Another common error is to use ordinary parentheses to construct a list reference when you should be using square or curly brackets, for example, if you say

$array = (1,2);

when you should have said

$array = [1,2];

The square brackets explicitly turn a list value into a scalar value, while parentheses do not. So when a parenthesized list is evaluated in a scalar context, the comma is treated like C's comma operator, which throws away the left argument, which is not what you want. See perlref for more on this.

This warning will not be issued for numerical constants equal to 0 or 1 since they are often used in statements like

1 while sub_with_side_effects();

String constants that would normally evaluate to 0 or 1 are warned about.

Perl wants to explain the problems to you. You just need to ask it what you're doing wrong :-)

like image 29
Dave Cross Avatar answered Apr 02 '26 13:04

Dave Cross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!