in reply to determine the variable causing the error: Use of uninitialized value

$ perl -wMstrict -e 'my $myvar; print $myvar; printf "%s", $myvar' Use of uninitialized value $myvar in print at -e line 1. Use of uninitialized value $myvar in printf at -e line 1.

Could you show some code that demonstrates the problem (SSCCE)? Also, which version of Perl are you using? Update: Just to be clear, by "the problem" I mean the variable name not showing up in the warning message. (Also, s/x/myvar/g)

Replies are listed 'Best First'.
Re^2: determine the variable causing the error: Use of uninitialized value
by AnomalousMonk (Archbishop) on Apr 13, 2017 at 18:55 UTC
    ... which version of Perl are you using?

    ruqui: This is an important question. The boon you seek was added with Perl version 5.10:

    c:\@Work\Perl>perl -wMstrict -le "print qq{perl version: $] \n}; ;; my $x; print $x; printf '%s', $x; " perl version: 5.008009 Use of uninitialized value in print at -e line 1. Use of uninitialized value in printf at -e line 1. perl version: 5.010001 Use of uninitialized value $x in print at -e line 1. Use of uninitialized value $x in printf at -e line 1.


    Give a man a fish:  <%-{-{-{-<

      AnomalousMonk it's correct what you say, but it only work with scalars, it doesn't work with more complex structures like hashes (or references to hashes):

      EXAMPLE:

      perl -wMstrict -le 'print qq{perl version: $] \n};;; my $x; my %y= (" +h"=>1, "i"=>2); print $x, $y{x}; printf "%s", $x;'
      OUTPUT:
      perl version: 5.018002 Use of uninitialized value $x in print at -e line 1. Use of uninitialized value in print at -e line 1. Use of uninitialized value $x in printf at -e line 1. </b>
        > it doesn't work with more complex structures like hashes (or references to hashes)

        To rephrase it, your problem are undefined elements of data structures like $h{x}.

        I don't think there is any out of the box solution and you'd need to send a feature request.

        As a side note: a hack could be to add a signal handler which parses the OP tree at the caller for all undefined entries .

        But that's far from trivial.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!