Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference in delayed evaluation of code in subroutines for Perl's 5.8 vs. 5.10 and 5.12?

Tags:

perl

perl5.8

This bit of code behaves differently under Perl 5.8 than it does under Perl 5.12:

my $badcode = sub { 1 / 0 };
print "Made it past the bad code.\n";

When I run it under 5.8, I get an error even though I never execute the division:

[brock@chase tmp]$ /usr/bin/perl -v  

This is perl, v5.8.8 built for i486-linux-gnu-thread-multi

[brock@chase tmp]$ /usr/bin/perl badcode.pl  
Illegal division by zero at badcode.pl line 1.

[brock@chase tmp]$ /usr/local/bin/perl -v  

This is perl 5, version 12, subversion 0 (v5.12.0) built for i686-linux  

[brock@chase tmp]$ /usr/local/bin/perl badcode.pl  
Made it past the bad code.

Under perl 5.10.1, it behaves as it does under 5.12:

brock@laptop:/var/tmp$ perl -v

This is perl, v5.10.1 (*) built for i486-linux-gnu-thread-multi

brock@laptop:/var/tmp$ perl badcode.pl  
Made it past the bad code.

I get the same results with a named subroutine, e.g.

sub badcode { 1 / 0 }

I don't see anything about this in the perl5100delta pod. Is this an undocumented change? A unintended side effect of some other change? (For the record, I think 5.10 and 5.12 are doing the Right Thing.)

like image 474
Brock Avatar asked May 04 '10 18:05

Brock


1 Answers

I believe this was planned, and I do see this mentioned in perl5100delta.pod:

Exceptions in constant folding

The constant folding routine is now wrapped in an exception handler, and if folding throws an exception (such as attempting to evaluate 0/0), perl now retains the current optree, rather than aborting the whole program. Without this change, programs would not compile if they had expressions that happened to generate exceptions, even though those expressions were in code that could never be reached at runtime. (Nicholas Clark, Dave Mitchell)

It just has do with the divided-by-zero exception not resulting in a compilation-stage abort.

like image 64
NO WAR WITH RUSSIA Avatar answered Sep 29 '22 12:09

NO WAR WITH RUSSIA