Perl: code difference
I am new to Perl and I am trying to find the difference between two snippets:
$string && $string eq 'foo'
$string eq 'foo'
I've tried several conditions, but I can't seem to find the difference. Can anyone help me determine the difference?
+3
Vaibhav Agarwal
source
to share
1 answer
One difference is that the second line can generate a warning message (when used use warnings;
) if a variable has never been assigned a value:
use warnings;
use strict;
my $string;
if ($string && $string eq 'foo') {
print "true1\n";
}
else {
print "false1\n";
}
if ($string eq 'foo') { # Get warning
print "true2\n";
}
else {
print "false2\n";
}
Warning message:
Use of uninitialized value $string in string eq
+12
toolic
source
to share