Where is the uninitialised value in the below code?
#!/usr/bin/perl
use warnings;
my @sites = (undef, "a", "b");
my $sitecount = 1;
my $url;
while (($url = $sites[$sitecount]) ne undef) {
$sitecount++;
}
Output:
Use of uninitialized value in string ne at t.pl line 6.
Use of uninitialized value in string ne at t.pl line 6.
Use of uninitialized value in string ne at t.pl line 6.
Use of uninitialized value in string ne at t.pl line 6.
You can’t use
undefin a string comparison without a warning.will raise a warning. If you want to test if a variable is defined or not, use:
Comments about the original question:
That’s a strange way to iterate over an array. The more usual way of doing this would be:
and drop the
$sitecountvariable completely, and don’t overwrite$urlin the loop body. Also drop theundefvalue in that array. If you don’t want to remove thatundeffor some reason (or expect undefined values to be inserted in there), you could do:If you do want to test for undefined with your form of loop construct, you’d need:
to avoid the warnings, but beware of autovivification, and that loop would stop short if you have
undefs mixed in between other live values.