[Tutor] defined()
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Tue Apr 4 04:06:28 CEST 2006
> > I'm interested in what use you would make of such a thing?
> My business partner is a perl programmer. He uses defined() a lot, I
> think, I've seen it in his code....
Hello!
The common idiom in Perl, I think, is to at least declare the variable,
even if one doesn't give an initial value, like this:
#############################################
## Perl pseudocode ##
use strict;
my $name;
## do things here that should initialize name
if (! defined($name)) {
# handle degenerate case here
}
#############################################
But this is very different than:
#############################################
## Perl pseudocode that doesn't use strict
## do things here that should initialize name
if (! defined($name)) {
# handle degenerate case here
}
#############################################
Now, if your business partner doesn't have the line 'use strict' in their
code, then give them a good kick and tell them to use it! It's criminal
for a professonal Perl programmer not to "use strict", and I feel almost
foolish about bringing this up. But it has to be said, just in case.
*grin*
In Python, the first assignment to a variable name has the same effect as
declaration, so the first Perl snippet has a translation like:
#############################################
## Python pseudocode
name = None
## do things here that should initialize name
if name is None:
## handle degenerate case here
#############################################
where we can use None as our uninitialized value.
Hope this helps!
More information about the Tutor
mailing list