Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Use of uninitialized variable with print << HTMLEND (and $ENV)

Reply
Thread Tools

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

 
 
kevinwhite@bellsouth.net
Guest
Posts: n/a
 
      10-31-2006
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

 
Reply With Quote
 
 
 
 
kevinwhite@bellsouth.net
Guest
Posts: n/a
 
      10-31-2006

(E-Mail Removed) wrote:
> 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" }

>
> Thanks in advance,
>
> -Kevin


 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      11-01-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
> 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;


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
kevinwhite@bellsouth.net
Guest
Posts: n/a
 
      11-02-2006
Michele Dondi wrote:
> On 31 Oct 2006 14:28:03 -0800, (E-Mail Removed) wrote:
>
> >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

>
> This shouldn't cause 'uninitialized' warnings. Unless you have
> variables interpolated in. In which case you may either take care of
> checking for defined()ness or *temporarily* go C<no warnings;>.
>
> >The second example is:
> >my $htmlfile = "";
> >my $hide = "";

>
> No need to initialize them, BTW.
>
> >$htmlfile = param('filename');
> >$hide = param('hide') || "no"; #<= what does this do?

>
> Sets $hide to "no" if param('hide') is a Perl false value. More about
> || in
>
> perldoc perlop
>
> >if ($htmlfile eq "default.html") #<= this causes an uninitialized
> >variable warning...

>
> Ditto as above!
>
> >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;

> ^^^^^^^^^^^^^
> ^^^^^^^^^^^^^
>
> (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
>
> >$n = 0;

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


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-02-2006
(E-Mail Removed) <(E-Mail Removed)> wrote:
> Michele Dondi wrote:
>> On 31 Oct 2006 14:28:03 -0800, (E-Mail Removed) wrote:
>>
>> >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

>>
>> This shouldn't cause 'uninitialized' warnings. Unless you have
>> variables interpolated in.



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


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing derived classes uninitialized variable to base class constructor !! PSN C++ 2 09-22-2006 04:14 PM
Use of uninitialized value in print Pam Perl Misc 2 09-19-2006 12:22 PM
Eclipse bug: Doesn't check for uninitialized variables when there are "too many" variable declarations. Oliver Wong Java 11 04-19-2006 09:40 AM
[FR/EN] variable non initalisée / Use of uninitialized value in substr ... Alextophi Perl Misc 2 09-14-2005 08:53 AM
Newbie. Use of uninitialized value in print?? Jon Anderson Perl Misc 7 04-03-2005 10:23 PM



Advertisments