Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Variable name missing in warning

by bigj (Monk)
on Apr 02, 2014 at 16:48 UTC ( [id://1080799]=note: print w/replies, xml ) Need Help??


in reply to Variable name missing in warning

Allthough I originally wrote a module like Test::Warn, I wasn't aware of that behaviour. However perldiag describes it as a kind of optimization:
Use of uninitialized value%s (W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables. To help you figure out what was undefined, perl will try to tell you the name of the variable (if any) that was undefined. In some cases it cannot do this, so it also tells you what operation you used the undefined value in. Note, however, that perl optimizes your program and the operation displayed in the warning may not necessarily appear literally in your program. For example, "that $foo" is usually optimized into ""that " . $foo", and the warning will refer to the "concatenation (.)" operator, even though there is no "." in your program.

Greetings,
Janek

Replies are listed 'Best First'.
Re^2: Variable name missing in warning
by LanX (Saint) on Apr 02, 2014 at 21:36 UTC
    I checked with B::Deparse and B::Concise and the variable name is clearly visible i.e not optimized away:

    > perl -MO=Deparse,-q use warnings; my (%a, %b, $c); my $x = "$a{'foo'} $b{'bar'} $c" __END__ use warnings; my(%a, %b, $c); my $x = $a{'foo'} . ' ' . $b{'bar'} . ' ' . $c; - syntax OK

    -q reveals the mentioned optimization of string-interpolations, but the variable name is not affected.

    I thought this is only a negligible bug, such that after fixing $a{foo} the following $b{bar} would be visible, but that's unfortunately not the case:

    > perl -w my (%a, %b, $c); $a{foo}="FIXED"; my $x = "$a{'foo'} $b{'bar'} $c"; __END__ Use of uninitialized value in concatenation (.) or string at - line 3. Use of uninitialized value $c in concatenation (.) or string at - line + 3.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Then why don't you (anybody in this thread) go read (and understand) the near-400-line subroutine of C code that tries to find variable names for undef warnings?

      See S_find_uninit_var in sv.c.

      The docs didn't say that sometimes variable names are optimized away (the mention of optimization was about the reported operation). The docs pretty clearly hint (to me, anyway) that finding the variable name isn't close to a trivial task and is known to fail somewhat often for reasons too complicated to usefully summarize in those docs.

      - tye        

        Is this a rhetoric question or do you really want to discuss why I don't do it?

        FWIW my best guess is that the 1 causes the recursive search to only match scalar values.

        14857             sv = find_uninit_var(o, uninit_sv, 1);

        But I am not a C programmer and far from understanding Perl guts.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re^2: Variable name missing in warning
by Anonymous Monk on Apr 02, 2014 at 17:23 UTC

    This says that optimizations (compile time optimizations I guess) can explain why perl doesn't know exactly in what operation the variable was used.

    But this doesn't say why perl would not know the name of the variable.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found