Re: Check Variables for NULL
by Tanktalus (Canon) on Feb 07, 2007 at 20:58 UTC
|
What is this NULL you speak of? Perhaps you have the wrong language? There is no NULL in Perl...
You probably want undef. That's the Perl equivalent of C/C++'s NULL. Check out defined.
if (defined $value)
{
Do that
}
else
{
Do this
}
You can use not to reverse that. | [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
|
|
Check the C standard. It has a NULL. How it's implemented is implementation defined, which means it could be anything. Section 7.1.6 of the 1990 standard (yes, there's a 1999 standard - I don't have a copy of that) includes the text:
The macros are
which expands to an implementation-defined null pointer constant
You're confusing C with C++ which defines NULL to be equivalent to the integer constant 0. Normally in C, the define is actually (void*)0.
| [reply] |
|
|
Re: Check Variables for NULL
by davorg (Chancellor) on Feb 08, 2007 at 09:16 UTC
|
Perl tries to make things as easy as possible for you and this is a good example.
In most cases, what you actually want is as simple as:
if ($value) {
# $value contains something useful
} else {
# $value is "empty"
}
(Note that the logic is reversed from your original example)
What we're doing here is checking the "truth" of $value. $value will be "false" if it contains any of the following values:
- "undef" (i.e. if it hasn't been given a value)
- The number 0
- The string "0"
- The empty string ("")
Any other value in $value will evaluate to "true".
This is all you need in most cases. There are a couple of "corner cases" that you sometimes have to consider:
- Sometimes 0 is an acceptable value. You account for that by using if ($value or $value == 0).
- Sonetimes you might want to test that $value contains nothing but whitespace characters. The easiest way to do that is by checking for the presence of non-whitespace characters with if ($value =~ /\S/).
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
Re: Check Variables for NULL
by Trihedralguy (Pilgrim) on Feb 07, 2007 at 21:13 UTC
|
sorry I didnt give enough information apparently.
Basically i perl script that pulls in data from the user. But I'm trying to write and if statement that will say something like "Hey, you didnt put any data in that box!" So I want to check if the $value is "0" or "blank" give the error, if it has data run the rest of the script. | [reply] |
|
|
Well the answer depends on how you get the value from the "box". Most UI components return empty input as an empty string, but some might return undef instead.
# let's say you've got the user input in $input..
if (defined($input) && $input ne "") {
# input is defined and not empty
}
else {
print "input wasn't filled in\n";
}
Note that compare strings with eq and ne instead of == and != since == and != coerce their arguments to numbers.
See equality operators and defined in the documentation.
| [reply] [d/l] [select] |
|
|
Your awesome!!
Thanks so much for your help, for some reason I just forgot about "EQ and NE" Duh!
| [reply] |
|
|
|
|
|
|
Re: Check Variables for NULL
by Moron (Curate) on Feb 08, 2007 at 14:35 UTC
|
The worst-case similar scenario I have encountered is where 0, '', undef(), "NULL", "null", "N/A" and "NA" all had to evaluate false and just about everything else as true. Commenting out my extra NA requirement would produce something like:
if ( True( $value ) ) {
}
else {
}
sub True {
defined $_[0]
and $_[0]
and ( uc( $_[0] ) ne "NULL" )
# and $_[0] !~ /^N\/?A$/
;
}
| [reply] [d/l] |