Use of uninitialized variable with print << HTMLEND (and $ENV)

K

kevinwhite

I am trying to fix some code I inherited, and there are many warnings I
would like to clean up. The biggest offenders are as follows:

print <<"HTMLEND"; #<= this causes an uninitialized variable warning...

<html><head></head><body></body></html>
HTMLEND

The second example is:
my $htmlfile = "";
my $hide = "";

$htmlfile = param('filename');
$hide = param('hide') || "no"; #<= what does this do?

if ($htmlfile eq "default.html") #<= this causes an uninitialized
variable warning...
{ #do some stuff
}

The third problem I have is printing environment variables when I have
"use strict" enabled. What is the legal way to do this?

I have:
my $n;
my @keys;
my @values;
my $Item;

@keys = keys %ENV;
@values %ENV;
$n = 0;
foreach $Item (@keys) { $$Item = $values[$n++];}
foreach $Item (@keys) { print "$Item=$$Item"; }

Thanks in advance,

-Kevin
 
K

kevinwhite

I am trying to fix some code I inherited, and there are many warnings I
would like to clean up. The biggest offenders are as follows:

print <<"HTMLEND"; #<= this causes an uninitialized variable warning...

<html><head></head><body></body></html>
HTMLEND

The second example is:
my $htmlfile = "";
my $hide = "";

$htmlfile = param('filename');
$hide = param('hide') || "no"; #<= what does this do?

if ($htmlfile eq "default.html") #<= this causes an uninitialized
variable warning...
{ #do some stuff
}

The third problem I have is printing environment variables when I have
"use strict" enabled. What is the legal way to do this?

I have:
my $n;
my @keys;
my @values;
my $Item;

@keys = keys %ENV;
@values %ENV;
$n = 0;
foreach $Item (@keys) { $$Item = $values[$n++];}
foreach $Item (@keys) { print "$Item=$$Item"; }

Got it:
foreach my $key (sort keys %ENV) { print "$key => $ENV{$key}\n" }
 
T

Tad McClellan

I am trying to fix some code I inherited, and there are many warnings I
would like to clean up. The biggest offenders are as follows:

print <<"HTMLEND"; #<= this causes an uninitialized variable warning...


No it doesn't.

<html><head></head><body></body></html>
HTMLEND


There are no variables there, so there can be no variables
with an undef value to trigger the warning.

Something else is going on, but since we have neither a complete
program, nor the actual text of the error message (hint), we
can do nothing to help you on this one...

The second example is:
my $htmlfile = "";
my $hide = "";

$htmlfile = param('filename');
$hide = param('hide') || "no"; #<= what does this do?


Selects a default value of "no" if there is no "hide" parameter.

if ($htmlfile eq "default.html") #<= this causes an uninitialized


There there must also have been no "filename" parameter.

Maybe you should select a default for that one too?

The third problem I have is printing environment variables when I have
"use strict" enabled. What is the legal way to do this?

I have:
my $n;
my @keys;
my @values;
my $Item;


You should instead declare your variables upon their first use,
rather than ahead of time.

@keys = keys %ENV;
@values %ENV;


my @values = values %ENV;
 
K

kevinwhite

Michele said:
This shouldn't cause 'uninitialized' warnings. Unless you have
variables interpolated in. In which case you may either take care of


No need to initialize them, BTW.


Sets $hide to "no" if param('hide') is a Perl false value. More about
|| in

perldoc perlop


Ditto as above!

^^^^^^^^^^^^^
^^^^^^^^^^^^^

(This is not valid Perl, I assume you mean

@values = values %ENV; #)

Yes, my bad - the system I am working on is not on a network, so I
don't have the luxury of cut/paste.

Thanks all for the help. It turns out the offending lines get reported
in kind of a strange way. If you have an if statement, with multiple
elsif, the first if check gets reported if any of the following elsif
parameters include an uninitialized variable. The same is true for
print <<HTMLEND; statements.

$hide = param('hide'); also causes uninitialized variables, so making
them my $hide = param('hide') || ""; was a better solution.

-Kevin

So far so fine as far as strict is concerned, although we recommend
people all the time to declare lexicals as close as possible to the
point where they're actually used
foreach $Item (@keys) { $$Item = $values[$n++];}
foreach $Item (@keys) { print "$Item=$$Item"; }

Here you have a symref, which is not making strict.pm happy. Just use
somehash instead.

But... what is it that you're trying to do? All in all it seems you're
wanting to do X but you're either really doing Y or doing X some very
awkward and convoluted way...


HTH,
Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
T

Tad McClellan

It turns out the offending lines get reported
in kind of a strange way.


And the "strange way" is common to all programming languages,
not just Perl.

If you have an if statement, with multiple
elsif, the first if check gets reported if any of the following elsif
parameters include an uninitialized variable.


Line numbers reported are when the language parser _notices_ the
error (or warning in this case).

When you get lost driving for example, you can report where it was
that you noticed that you were lost, but you can't really report
where it was that you made the wrong turn. Similarly for parsers.
 

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,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top