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