Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

no uninitialized warnings

by si_lence (Deacon)
on Aug 25, 2004 at 12:42 UTC ( [id://385666]=perlquestion: print w/replies, xml ) Need Help??

si_lence has asked for the wisdom of the Perl Monks concerning the following question:

Dear fellow monks, I have this script that reads a rather lengthy text file line by line and extracts information from it.
While developing I want to see the values that are put into different variables at the time, so I print them out to STDOUT.

So far everything is fine. But I do get a lot of "Use of uninitialized value in concatenation (.) or string" warnings because the values are in fact uninitialized. The way I got rid of the warnings is:
{ no warnings "uninitialized"; print "var1=$var1 -- var2=$var2 -- var3=$var3\n"; }
This looks just a bit clumsy to me. Is there a better way to achieve this?

si_lence

Replies are listed 'Best First'.
Re: no uninitialized warnings
by herveus (Prior) on Aug 25, 2004 at 13:06 UTC
    Howdy!

    What do you want displayed when the variable is uninitialized? Your approach is straightforward and clear.

    Data::Dumper provides another mechanism that will show uninitialized values as "undef" without requiring you to write lots of code.

    yours,
    Michael
Re: no uninitialized warnings
by ccn (Vicar) on Aug 25, 2004 at 12:46 UTC

    Why don't you just initialize them?

    $_ ||= '' for $var1, $var2, $var3; print "var1=$var1 -- var2=$var2 -- var3=$var3\n";
    or
    print "var1=", $var1 || '', " -- var2=", $var2 || '', " -- var3=", $va +r3 || '', "\n";
      Danger, Will Robinson. Danger!

      If $var1 has the value 0, it will be set to '', which might not be what you want.

        Mh... something like this?

        @x=(1..3) ; undef $x[1] ; $_ = defined $_? $_: '[__UNDEF__]' for @x ; print "@x" ;

        Ciao!
        --bronto


        The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
        --John M. Dlugosz

      If the values might be uninitialized in the normal course of operation, and it's acceptible, then I see no problem just turning off the warning, as the OP has done. This is Perl, after all. The language should do what we want, not the other way around. :-)

Re: no uninitialized warnings
by Forsaken (Friar) on Aug 26, 2004 at 09:56 UTC
    well, i'm one of those people that considers warnings blasphemy, so i avoid them like the plague. In a case like this I'd simply use something like:

    print "some sort of introduction of the following comes here";
    if($var1) { print "\$var1 = $var1 "; }
    if($var2) { print "\$var2 = $var2 "; }
    if($var3) { print "\$var3 = $var3"; }
    print "\n";

    perhaps i'm allowing the quirks of the language to dominate my style here, but I have all my bases covered this way :-)

    Additionally, I tend to encapsulate a construction like this in a if($debug) {} so as to allow me to turn all the debug messages in a script on or off with the change of a single variable somewhere.
Re: no uninitialized warnings
by si_lence (Deacon) on Aug 26, 2004 at 12:29 UTC
    Thanks for all the input! I used Data::Dumper to print out larger data structures, but using it to print out the names is very nice too.
    print Data::Dumper->Dump([$var1, $var2, $var3], [qw($var1 $var2 $v +ar3)]);


    Speaking of a $debug switch to print out information only while debugging:
    I usually use an if statement modifier so I can use more than one level of verbosity
    my $v=0; #0 for silent, 1 for main info, 2 for all info ... print "something important\n" if $v > 0; print "something not so important\n" if $v >1;
    si_lence

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://385666]
Approved by gellyfish
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 19:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found