how to report which specific variable is uninitialized

M

Morfys

Hi,

When running perl with the -w option, and executing a "print'" with
about 30 variables, I get

Use of uninitialized value in print at at ./temp.pl line 9.

Is there any way to make perl report the exact variable name (out of
the 30) that is not initialized?

' use diagnostics;' doesn't help.

For various reasons, writings 30 statements of the form
defined ($var) or warn "var is undefined";
is difficult/a pain in my case.

Thanks.
 
B

Ben Morrow

Quoth Morfys said:
Hi,

When running perl with the -w option, and executing a "print'" with
about 30 variables, I get

Use of uninitialized value in print at at ./temp.pl line 9.

Is there any way to make perl report the exact variable name (out of
the 30) that is not initialized?

~% perl5.8.8 -we'my $x; print $x'
Use of uninitialized value in print at -e line 1.
~% perl5.10.0 -we'my $x; print $x'
Use of uninitialized value $x in print at -e line 1.

See if you can upgrade to 5.10.0.

Ben
 
S

Sherm Pendley

Morfys said:
When running perl with the -w option, and executing a "print'" with
about 30 variables, I get

Use of uninitialized value in print at at ./temp.pl line 9.

Is there any way to make perl report the exact variable name (out of
the 30) that is not initialized?

' use diagnostics;' doesn't help.

Are you using strict? That requires you to declare your variables
before using them, so it will tell you if the "unitialized"
variable is really a variable whose name you've misspelled in the
call to print().
For various reasons, writings 30 statements of the form
defined ($var) or warn "var is undefined";
is difficult/a pain in my case.

Honestly, I don't see why. Sure, it would be a pain to write all that
boilerplate all the time, but doing it once for debugging purposes
shouldn't be too much of a chore. It's just pasting the same line 30
times and then editing the variable names. Unless you're using some
sort of assistive input technology that's very time-consuming, it
shouldn't take more than a few minutes to do that kind of edit.

sherm--
 
M

Morfys

Are you using strict? That requires you to declare your variables
before using them, so it will tell you if the "unitialized"
variable is really a variable whose name you've misspelled in the
call to print().

Yes, I am using strict. The variables have all been declared.
Honestly, I don't see why. Sure, it would be a pain to write all that
boilerplate all the time, but doing it once for debugging purposes
shouldn't be too much of a chore.

Ok, I just thought there might some cool way to do it in perl with one
line. ;)
 
J

Jürgen Exner

Morfys said:
When running perl with the -w option, and executing a "print'" with
about 30 variables, I get

Use of uninitialized value in print at at ./temp.pl line 9.

Is there any way to make perl report the exact variable name (out of
the 30) that is not initialized?
For various reasons, writings 30 statements of the form
defined ($var) or warn "var is undefined";
is difficult/a pain in my case.

If it's for debugging purposes only then it takes only 5 attempts using
bisection.
Put 15 of those variables into one print() statement. If this statement
pops the error, then you know it's one of those 15, otherwise it's one
of those 15 you didnt' include. Cut that number in half and use 8 of the
remaining suspects in this print() statement and repeat the process to
reduce the number further. After 5 iterations you've nailed the bad guy.

jue
 
D

Dr.Ruud

Morfys schreef:
When running perl with the -w option, and executing a "print'" with
about 30 variables, I get

Just put them in a more appropriate data structure, see perldata.
 
X

xhoster

Jürgen Exner said:
If it's for debugging purposes only then it takes only 5 attempts using
bisection.
Put 15 of those variables into one print() statement. If this statement
pops the error, then you know it's one of those 15, otherwise it's one
of those 15 you didnt' include. Cut that number in half and use 8 of the
remaining suspects in this print() statement and repeat the process to
reduce the number further. After 5 iterations you've nailed the bad guy.

I'd do it in fewer attempts by n-section rather than bisection.

First I'd look at the output and see which position had an empty string.
Unless there is more than one such, then the one that is empty is the
one that is undef.

Second, I'd copy the print statement and change it to a foreach:
print "$list,$of,$variables,$one,$of,$which,$is,$undefined\n";

Becomes

my $i=1;
foreach ($list,$of,$variables,$one,$of,$which,$is,$undefined) {
warn "It's the $i th one" unless defined $_;
$i++;
};



But zeroth, I almost certainly avoid having 30 variables explicitly in a
print statement, by using a different data structure.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
N

nntpman68

Is there any way to make perl report the exact variable name (out of
the 30) that is not initialized?

The problem can be reduced by choosing better data structures and
debugging can be done by bi-secting or n-secting a print statements,

Something which I do also sometimes is a statement like this

my @tmp = ($a,$b,$c); # who is not defined ?
print((map { defined $_ ? 1 : 0 } (@tmp)),"\n");
You'll get a 1 for each defined and a 0 for each undefined variable.

normally I just copy the offending line, remove all but the variable names,


But:
Would none of you wish to have a run time error message, that is a
little more explicit.

I don't know how difficult it would be for a future perl version to have
more specific error reporting.

It's probably not that simple, otherwise it would be already there.


Just imagine an error message saying:
"uninitialized value in print at line 10 ($a was not initialized)

I think it would increase productivity.


Does anybody know how perl 6 would react?

bye



N
 
B

Ben Morrow

Quoth (e-mail address removed):
But:
Would none of you wish to have a run time error message, that is a
little more explicit.

I don't know how difficult it would be for a future perl version to have
more specific error reporting.

It's probably not that simple, otherwise it would be already there.

As I said, this has already gone into 5.10.

Ben
 
T

Tad J McClellan

Morfys said:
Yes, I am using strict. The variables have all been declared.


Ok, I just thought there might some cool way to do it in perl with one
line. ;)


If you had chosen a better data structure to start with, then there
would some cool way to do it with two lines.


foreach my $varname ( sort keys %vars ) {
print "'$varname' is not defined\n" unless defined $vars{$varname};
}

But then you would have had to use hash keys in place of variable names.

Instead of
$sales_tax = stuff();
use
$vars{sales_tax} = stuff();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top