delayed evaluation of code in subroutines - 5.8 vs. 5.10 and 5.12

Posted by Brock on Stack Overflow See other posts from Stack Overflow or by Brock
Published on 2010-05-04T18:59:48Z Indexed on 2010/05/04 19:18 UTC
Read the original article Hit count: 430

Filed under:

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";
[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.)

© Stack Overflow or respective owner

Related posts about perl