Why does Perl's string multiplication not warn on undef?
Asked Answered
P

1

20

Under

use strict;
use warnings;
my $foo = undef;

the expressions

$foo . ''

and

"$foo"

both produce

Use of uninitialized value $foo in ... at ...

but the following expression gives the empty string without warning:

$foo x 1

Anyone knows why? I mean, it's cool to have a nice stringification idiom ($_ x 1 is quite much shorter than defined ? "$_" : ''), but it feels a bit weird.

Pride answered 9/10, 2012 at 13:45 Comment(2)
I'm sure it wasn't intentional, at least not initially.Wehrle
perldoc perlop (search for "Multiplicative Operators") doesn't say anything about the behavior of x with an undef left operand. I agree it's probably unintentional.Pillbox
W
9

I once saw a show with a scene in a law school class. The instructor presented two similar cases to his students and asked why did they result in different rulings. It's because the rulings were made by judges, and judges are people, he eventually explained.

That should warn. The developers that work on Perl are people. Run perlbug to send a bug report if you wish.

Wehrle answered 9/10, 2012 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.