don't do this

nicholas on 2010-02-16T18:03:08

It's just occurred to me that it might be possible to goto \&sub_ref out of a BEGIN block. After all, they're really subroutines at heart. So, does it work?

$ cat sick.pl
#!perl -w
use strict;

sub sick {
    warn "Yuck!";
}

BEGIN {
    goto \&sick;
}

__END__
$ perl sick.pl
Yuck! at sick.pl line 5.

Yep. Sure does.

Don't do this.

This is where someone points me at prior art from 10 years ago. There is, after all, nothing new under the sun.


Why not?

bart on 2010-02-16T19:25:47

I don't see why one shouldn't be allowed to do this, as it's not really different from plain calling

BEGIN {
     &sick;   # or sick();
}

As part of the source hasn't been compiled (or parsed) yet, you can only jump — or call — backwards.

Re:Why not?

tsee on 2010-02-17T08:42:49

goto &sub doesn't return to its previous callframe. I'm not quite sure how that pans out during compilation. I think that's part of what's worrying Nicholas. :)