perl school; undef, legit use of

I often test subs for an undef returns (return undef).  
Is it legal to test scalars   $x == undef?  It seems that
only a test against an explictly assigned value of 'undef'
is meaningful.

If not, ignor the rest of the question.


==============================================

Here a program where I test every state of $x I could think of.  Here's
the output:
  Is nothing undefined:  yes                            
  Is an unassigned declaraton undefined:  yes  # $x;
  Is an undefed var undefined:  yes                 # undef $x;
  Is a string var undefined:  yes                      # $x = "sdfsdfsdfsdf";
  Is a one var undefined:  no                          # $x = 1;
  Is var assigned to undef,  undefined:  yes      # $x = undef;
  Is var assigned to !undef,  undefined:  no      # $x = !undef;

==================================
print qq/Is nothing undefined:  /;
if ($x == undef) {
  print qq/yes\n/;
}else {
  print qq/no\n/;
}

$x;
print qq/Is an unassigned declaraton undefined:  /;
if ($x == undef) {
  print qq/yes\n/;
}else {
  print qq/no\n/;
}

undef $x;
print qq/Is an undefed var undefined:  /;
if ($x == undef) {
  print qq/yes\n/;
}else {
  print qq/no\n/;
}

$x = "this is a string";
print qq/Is a string var undefined:  /;
if ($x == undef) {
  print qq/yes\n/;
}else {
  print qq/no\n/;
}

$x = 1;
print qq/Is a one var undefined:  /;
if ($x == undef) {
  print qq/yes\n/;
}else {
  print qq/no\n/;
}

$x = undef;
print qq/Is var assigned to undef,  undefined:  /;
if ($x == undef) {
  print qq/yes\n/;
}else {
  print qq/no\n/;
}

$x = !undef;
print qq/Is var assigned to !undef,  undefined:  /;
if ($x == undef) {
  print qq/yes\n/;
}else {
  print qq/no\n/;
}



LVL 1
hank1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hank1Author Commented:
I might add that if $x = 1
and $x == undef returns true, wouldn't a sub fake out a test for undef?
0
ozoCommented:
with ==, undef acts like 0
with eq, undef acts like ''
to test if a variable is defined, use
defined $x

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.