Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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?


In reply to Re: determine the variable causing the error: Use of uninitialized value by marinersk
in thread determine the variable causing the error: Use of uninitialized value by ruqui

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found