> Sorry to bother you, since I notice other people have also 
> been asking 
> about defined.  But the answers to their questions have not 
> helped me to 
> this point.

  No problem.  That's what we're here for.  As long as you've put in the
time to try to solve the problem yourself (which it looks like you've
done), you're not bothering anyone!

> 
> This morning I started seeing a vague, unhelpful warning 
> message about an 
> undefined variable from a program after months of successful 
> use.  The 
> warning occurs in a test line of the form
> 
>         if ($xx > $yy) {
> 
> So I 
> 
>   1.  Checked the input data visually.
> 
>   2.  Checked the data again by going through the algorithm, 
>        using the actual data in the input files.
> 
>   3.  Added print statements for $xx and $yy.
> 


  What did you see when you printed $yy?


> Finding nothing wrong, I created the following test program 
> containing the 
> essential logic, and with a variable deliberately undefined.
> 

  What are you really trying to fix.  Did the logic of your code break?
Or are you merely concerned with the warning message?  Perl is only
doing what you asked it to do.  By using the 'warnings' pragma, you've
specifically asked Perl to warn you when you use a variable that has not
been defined yet.  To remove the warning message, remove the "use
warnings;" line from your code!
  However, the warning is valuable.  You are probably assuming that $yy
== 0, which it most certainly is not.  It is "undefined".  If you want
your code to assume that this ($yy) variable is zero when it is
undefined, why not put in a test and change it to zero if it is
"undefined"?

  $yy = 0 unless( defined $yy );

> ==============
> use strict;
> use warnings;
> use diagnostics;
>
> my $xx = 7;
> my $yy;
>
> if ($xx > $yy) {
>         print "xx > yy, so do stuff\n";
> } else {
>         print "Do not do stuff\n";
> }
> ==============

  <<SNIP>>

> ==============
> use strict;
> use warnings;
> use diagnostics;
> 
> my $xx = 7;
> my $yy;
> 
> if ((defined $yy) == 0) {


  by the way, this is easier to say like this:
   if (!defined $yy)
  or better yet:
   unless( defined $yy )


>         print "\nyy is NOT defined, so Exit\n";
>         exit;
> }
> 
> if ((defined $xx) == 0) {
>         print "\nxx is NOT defined, so Exit\n";
>         exit;
> }
> 
> if ($xx > $yy) {
>         print "xx > yy, so do stuff\n";
> } else {
>         print "Do not do stuff\n";
> }
> ==============

> 
> In the test program, this fixed the problem, as did several 
> other versions 
> of the tests.  

  What problem was solved?  I'm confused here.

> So I added the tests to the real program, 
> intending to add 
> a bunch of print statements before "exit" so I could analyze the 
> underlying problem.  Unfortunately, the tests using "defined $yy" and 
> "defined $xx" failed in the real program:  absolutely no change in 
> behavior, except that the line number printed by the warning message 
> increased to allow for the test code.  I tried a whole bunch 
> of tests, and 
> every single one of them failed.
> 
> What have I missed?
> 
> Thanks,
> Walt
> 
> -- 

Hope to help! 

--Errin

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to