Perl: code difference
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
source to share