For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > September 2005 > Newbie: Testing for Null









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Newbie: Testing for Null
Pritchie

2005-09-27, 7:00 pm

How do I test for null values?

eg.

my $MyVal;
print "MyVal:".$MyVal;

gives me...

Use of uninitialized value in concatenation (.) or string at
./process-images.pl line 80 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a
mistake.
To suppress this warning assign a defined value to your variables.

To help you figure out what was undefined, perl tells you what
operation
you used the undefined value in. Note, however, that perl optimizes
your
program and the operation displayed in the warning may not necessarily
appear literally in your program. For example, "that $foo" is
usually optimized into "that " . $foo, and the warning will refer to
the concatenation (.) operator, even though there is no . in your
program.

apart from commenting out "use warnings" etc and assign a defined value.
How can I test for null or undef?

so if I did something like
my $MyVal = $ARGV[0]
but didn't pass an argument to the script, I could catch and handle the
problem.

Thanks all



Paul Lalli

2005-09-27, 7:00 pm

Pritchie wrote:
> How do I test for null values?


There is no such thing as a "null" value in Perl.
There is the empty string:
my $empty = '';
There is an undefined value;
my $undefined;
(or)
my $undefined = undef;
There is a string containing only the Null byte:
my $null = "\0";
There is the number zero:
my $zero = 0;

> apart from commenting out "use warnings" etc and assign a defined value.
> How can I test for null or undef?
>
> so if I did something like
> my $MyVal = $ARGV[0]
> but didn't pass an argument to the script, I could catch and handle the
> problem.


perldoc -f defined

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com