how to report which specific variable is uninitialized

Discussion in 'Perl Misc' started by Morfys, Aug 21, 2008.

  1. Morfys

    Morfys Guest

    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.
     
    Morfys, Aug 21, 2008
    #1
    1. Advertising

  2. Morfys

    Ben Morrow Guest

    Quoth 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?


    ~% 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

    --
    If you put all the prophets, | You'd have so much more reason
    Mystics and saints | Than ever was born
    In one room together, | Out of all of the conflicts of time.
    The Levellers, 'Believers'
     
    Ben Morrow, Aug 21, 2008
    #2
    1. Advertising

  3. Morfys <> writes:

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

    --
    My blog: http://shermspace.blogspot.com
    Cocoa programming in Perl: http://camelbones.sourceforge.net
     
    Sherm Pendley, Aug 21, 2008
    #3
  4. Morfys

    Morfys Guest

    On Aug 21, 6:53 pm, Sherm Pendley <> wrote:
    > Morfys <> writes:
    > > 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().


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

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


    Ok, I just thought there might some cool way to do it in perl with one
    line. ;)
     
    Morfys, Aug 21, 2008
    #4
  5. Morfys <> wrote:
    >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
     
    Jürgen Exner, Aug 21, 2008
    #5
  6. Morfys

    Dr.Ruud Guest

    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.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Aug 21, 2008
    #6
  7. Morfys

    Guest

    Jürgen Exner <> wrote:
    > Morfys <> wrote:
    > >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.


    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.
     
    , Aug 21, 2008
    #7
  8. wrote:

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


    Amen to that!

    jue
     
    Jürgen Exner, Aug 21, 2008
    #8
  9. Morfys

    nntpman68 Guest

    > 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

    Morfys wrote:
    > 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.
     
    nntpman68, Aug 21, 2008
    #9
  10. Morfys

    Ben Morrow Guest

    Quoth :
    >
    > 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

    --
    Razors pain you / Rivers are damp
    Acids stain you / And drugs cause cramp. [Dorothy Parker]
    Guns aren't lawful / Nooses give
    Gas smells awful / You might as well live.
     
    Ben Morrow, Aug 21, 2008
    #10
  11. Morfys <> wrote:
    > On Aug 21, 6:53 pm, Sherm Pendley <> wrote:
    >> Morfys <> writes:
    >> > 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().

    >
    > Yes, I am using strict. The variables have all been declared.
    >
    >> > 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.

    >
    > 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();


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Aug 22, 2008
    #11
  12. Morfys

    Peter Scott Guest

    On Thu, 21 Aug 2008 10:00:46 -0700, Morfys wrote:
    > On Aug 21, 6:53 pm, Sherm Pendley <> wrote:
    >> Morfys <> writes:
    >> > 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?

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


    There is. The line is:

    use 5.010;

    :)

    --
    Peter Scott
    http://www.perlmedic.com/
    http://www.perldebugged.com/
     
    Peter Scott, Aug 22, 2008
    #12
    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. Oliver Wong
    Replies:
    11
    Views:
    932
    Hendrik Maryns
    Apr 19, 2006
  2. PSN
    Replies:
    2
    Views:
    326
  3. asit

    uninitialized variable

    asit, Jun 9, 2009, in forum: Java
    Replies:
    13
    Views:
    659
    John B. Matthews
    Jun 17, 2009
  4. Philipp Klaus Krause

    Value of uninitialized variable

    Philipp Klaus Krause, Jan 19, 2010, in forum: C Programming
    Replies:
    27
    Views:
    2,479
    Ben Bacarisse
    Jan 23, 2010
  5. Gavin Sinclair
    Replies:
    9
    Views:
    150
    Martin DeMello
    Dec 29, 2003
Loading...

Share This Page