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

Discussion in 'Perl Misc' started by kevinwhite@bellsouth.net, Oct 31, 2006.

  1. Guest

    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
     
    , Oct 31, 2006
    #1
    1. Advertising

  2. Guest

    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
     
    , Oct 31, 2006
    #2
    1. Advertising

  3. <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 1, 2006
    #3
  4. Guest

    Michele Dondi wrote:
    > On 31 Oct 2006 14:28:03 -0800, 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,
     
    , Nov 2, 2006
    #4
  5. <> wrote:
    > Michele Dondi wrote:
    >> On 31 Oct 2006 14:28:03 -0800, 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 2, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ankit Mehta
    Replies:
    1
    Views:
    1,439
    Simon Brooke
    Sep 29, 2006
  2. TDR
    Replies:
    3
    Views:
    180
    Daniel Berger
    Aug 31, 2007
  3. Jon Anderson

    Newbie. Use of uninitialized value in print??

    Jon Anderson, Apr 3, 2005, in forum: Perl Misc
    Replies:
    7
    Views:
    235
  4. Alextophi
    Replies:
    2
    Views:
    144
    Alextophi
    Sep 14, 2005
  5. Pam
    Replies:
    2
    Views:
    287
    Brian McCauley
    Sep 19, 2006
Loading...

Share This Page