Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check If a Variable is NULL

Status
Not open for further replies.

gnikol1

IS-IT--Management
Jan 30, 2002
29
EU
Can you tell me how can

I check if a variable is Null or not

thanks a lot
 
By null I assume you mean undef. There are two ways primarily, which are:

if(!$var){} # which also test whether 0 or an empty string too

or

if(undef($var)) {}

HTH,
Barbie. Leader of Birmingham Perl Mongers
 
Thanks a lot it somethng that I thouht so

 
I always use:

if(defined($var)){
# then do something
}

Used to be (Perl 4) that referring to a variable or element of a hash or array would cause that variable to spring into existence. Does that still apply? No idea. :) Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
That depends on how you refer to it, I think. If you just check it in an if statement, I don't think it springs into existence. If you actually use it's value, then it does. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Nope, doesn't do it anymore, following code using 5.6.1 prints out "undef" three times.

print "defined\n" if(defined($var));
print "undef\n" if($var == undef);
print "defined\n" if(defined($var));
print "undef\n" if($var eq undef);
print "defined\n" if(defined($var));
print "undef\n" unless($var);
print "defined\n" if(defined($var));
Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
That looks like it proves what I just said, if you use it in an if statement only it doesn't spring into existence. All the statements above use the variable only in an if (or unless) statement. Try using the variable in a statement like one of these and see what happens (I haven't bothered to try):
Code:
print $var;
$var2 = $var;
Either way it doesn't seem to really matter. If the variable DOESN'T spring into existence and you try to use it, it will still be undefined and have a string value of null.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
I use the following, very simple

if ($scalor eq "") {
$test = 1;
}
else {
$test = 0;
}
Now $test bit holds info whether or not variable has any data in it.

So then
if ($test == 1) {
print "I am sorry, you did not enter any data in the field";
exit;
}
else {
print "Data sent successfully"
etc..
etc..
}

Hope that helps
Code to do what ever you want with variable





Also good for checking form input to see if all fields filled out by inserting a counter.

if ($scalor =
 
You don't actually need to say:
Code:
if ($scalar eq "")
Since a null string is considered FALSE, this does the same thing:
Code:
if ($scalar)
And here's a one-liner to do the same thing:
Code:
$test = $scalar ? 0 : 1;
To see if a variable has an data in it you really don't need to set the $test variable at all. Just do this (unless is the same as "if not", or you can think of it as "reversing" the if and else parts):
Code:
unless ($scalar) {
print "I am sorry, you did not enter any data in the field";
exit;
}
Note that since the above statement exits if true, you don't really need an else part at all. No other code will be executed if true, so anything after the if statement will ONLY be executed on a "else" condition. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Sorry Tracy, I put that badly - I was agreeing with you. Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top