Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: determine the variable causing the error: Use of uninitialized value

by marinersk (Priest)
on Apr 14, 2017 at 03:44 UTC ( [id://1187906]=note: print w/replies, xml ) Need Help??


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

Okay, I'm playing around with this just for funz and learninz and I've hit something I'm having trouble explaining: Why does evalnot detect this as an error?

Original command placed in script and executed:

#!/usr/bin/perl use strict; use warnings; print qq{perl version: $] \n};;; my $x; my %y= ("h"=>1, "i"=>2); print $x, $y{x}; printf "%s", $x; exit; __END__

Produces, as advertised:

U:\>uninit01.pl perl version: 5.018002 Use of uninitialized value $x in print at U:\uninit01.pl line 10. Use of uninitialized value in print at U:\uninit01.pl line 10. Use of uninitialized value $x in printf at U:\uninit01.pl line 11.

So I tried to capture the error in an evalblock:

#!/usr/bin/perl use strict; use warnings; print qq{perl version: $] \n};;; my $x; my %y= ("h"=>1, "i"=>2); eval { print $x, $y{x}; } ; # Check for error out of eval{} if ($@) { # There was an error trapped. my $dsperr = $@; print "-------------------------\n"; print "$dsperr\n"; print "-------------------------\n"; } else { print "No error found.\n"; } printf "%s", $x; exit; __END__

But this produced:

U:\>uninit02.pl perl version: 5.018002 Use of uninitialized value $x in print at U:\uninit02.pl line 12. Use of uninitialized value in print at U:\uninit02.pl line 12. No error found. Use of uninitialized value $x in printf at U:\uninit02.pl line 29.

What is it about evalthat I'm not understanding properly here?

Replies are listed 'Best First'.
Re^2: determine the variable causing the error: Use of uninitialized value
by AnomalousMonk (Archbishop) on Apr 14, 2017 at 04:28 UTC

    ... uninitialized value ... is a warning, not an error (unless it's escalated to FATAL-ity); no exception is thrown.


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

      Thank you -- That was the closest thing I could come up with, so I should trust my instincts more.

      So --

      Since you didn't mention one, I presume there isn't an evalequivalent which does catch warnings?

        You could use

        { local $SIG{__WARN__} = sub { ... }; eval { ... }; }

        to trap the warning and do something special with it. However, I can't see that helping with identifying uninitialised values.

        There's example code in perlvar: %SIG.

        — Ken

        "...catch warnings?"

        With use warnings FATAL => qw(uninitialized); it works with eval as well as with Try::Tiny. As AnomalousMonk suggested.

        «The Crux of the Biscuit is the Apostrophe»

        Furthermore I consider that Donald Trump must be impeached as soon as possible

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 07:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found