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.

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.

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

==============
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";
}
==============

Running this program, of course, yields a warning that something is 
undefined in the line

        if ($xx > $yy) {

So then I added what seems to be a plausible test condition, as shown in 
this version:

==============
use strict;
use warnings;
use diagnostics;

my $xx = 7;
my $yy;

if ((defined $yy) == 0) {
        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.  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

-- 
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