my $foo;
. . . the value of $foo is undef. This is an actual value that is different from 0 and an empty string, and can be tested for.
Perl will say nothing when you declare the variable and will assign undef to it.
At that point you can test to see whether or not it has been given a defined value:
if ( defined( $foo ) )
Perl will then do the right thing when you use the variable, i.e. assigning it the value of zero if you use it as a number, or an empty string if you use it as a string. If you have warnings enabled, this is where Perl will squawk:
#!/usr/bin/perl
use strict;
use warnings;
my $foo;
print $foo * 3;
Use of uninitialized value $foo in multiplication (*) at -e line 1.
0
Hope this helps
Edit: added example of defined(), warnings output
The way forward always starts with a minimal test.
|