Help - Search - Member List - Calendar
Full Version: Defined revisited
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Walter A Poor Jr
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 stuffn";
} else {
print "Do not do stuffn";
}
==============

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 Exitn";
exit;
}

if ((defined $xx) == 0) {
print "nxx is NOT defined, so Exitn";
exit;
}

if ($xx > $yy) {
print "xx > yy, so do stuffn";
} else {
print "Do not do stuffn";
}
==============

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

Errin M HMMA/IT Larsen
QUOTE
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!

QUOTE

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?


QUOTE
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 );

QUOTE
=============> use strict;
use warnings;
use diagnostics;

my $xx = 7;
my $yy;

if ($xx > $yy) {
print "xx > yy, so do stuffn";
} else {
print "Do not do stuffn";
}
=============
<<SNIP


QUOTE
=============> 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 )


QUOTE
print "nyy is NOT defined, so Exitn";
exit;
}

if ((defined $xx) == 0) {
print "nxx is NOT defined, so Exitn";
exit;
}

if ($xx > $yy) {
print "xx > yy, so do stuffn";
} else {
print "Do not do stuffn";
}
=============

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

What problem was solved? I'm confused here.

QUOTE
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


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.