Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Cloaked eval errors - bug or feature?

Reply
Thread Tools

Cloaked eval errors - bug or feature?

 
 
Bas van Sisseren
Guest
Posts: n/a
 
      08-09-2005
Hi,

I recently found out i was using a 'local $@' and a 'die' in the same code
block by mistake. The result was an eval error which was cloaked by the
'local $@'. It seems to me this is not the behaviour someone would expect
of this combination.

I'm using perl v5.8.7 from debian sid.

Some examples:
=====[ Example #1 ]=====
#!/usr/bin/perl -w

$@='';

eval {
print "1\n";
eval {
print "2\n";
{
local $@;
die "something";
}
print "3\n";
};
die $@ if $@;
print "4\n";
};

if ($@) {
print "Exception caught: $@";
} else {
print "OK\n";
}
=======[ Result ]=======
1
2
4
OK
========================

The exception isn't caught in the 'die $@ if $@', therefore it isn't
possible anymore to rethrow this exception. It seems like the inner eval
didn't have any problems.

=====[ Example #2 ]=====
#!/usr/bin/perl -w

$@='';

eval {
print "1\n";
eval {
$@="hi there";
print "2\n";
{
local $@;
# die "something";
}
print "3\n";
};
die $@ if $@;
print "4\n";
};

if ($@) {
print "Exception caught: $@";
} else {
print "OK\n";
}
=======[ Result ]=======
1
2
3
4
OK
========================
Normal, expected, behaviour. At the end of the outer eval, the $@ variable
is cleared.

=====[ Example #3 ]=====
#!/usr/bin/perl -w

$@='';

eval {
print "1\n";
eval {
$@="hi there";
print "2\n";
{
local $@;
die "something";
}
print "3\n";
};
die $@ if $@;
print "4\n";
};

if ($@) {
print "Exception caught: $@";
} else {
print "OK\n";
}
=======[ Result ]=======
1
2
Exception caught: hi there at ./eval_test.pl line 16.
========================
Now, the previous $@ is inserted back into the $@ variable. We now have
caught an exception in the outer eval, which was never thrown.


Solution?

I think the $@ should not be set in the code-block of the die itself, but at
the point where code execution resumes, at the end of the eval{} block.

--
Bas van Sisseren
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: ETs arrive, cloaked as red rain Art Deco Computer Support 0 06-24-2006 03:37 AM
Cross referencing Sony rootkit cloaked CLSID WinXP registry keys Pamela Fischer Computer Support 11 11-23-2005 07:19 AM
Favicon for cloaked site in virtual directory Martin Maat ASP .Net 0 05-30-2004 03:01 PM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM
Virus cloaked as a Microsoft update John Pusinsky Digital Photography 33 10-01-2003 05:05 PM



Advertisments