By the way, if that BEGIN block will end up being use statement, use subs is not needed. For example,
override.pm:
use strict;
use warnings;
package override;
sub import {
my ($class, $sub_name, $sub) = @_;
my $pkg_name = caller();
my $pkg_sym = _pkg_symtab($pkg_name);
delete $pkg_sym->{$sub_name};
if ($sub) {
no strict 'refs';
*{"${pkg_name}::$sub_name"} = $sub;
}
}
sub _pkg_symtab {
my ($pkg) = @_;
my $p = \%::;
$p = $p->{"${_}::"} for split /::/, $pkg;
return $p;
}
1;
test.pl:
use strict;
use warnings;
open(my $fh, '<', $0); # Calls CORE::open
use override open => sub { print "Hi\n"; };
open(my $fh2, '<', $0); # Prints Hi
use override open => sub { print "Ho\n"; };
open(my $fh4, '<', $0); # Prints Ho
use override open => undef;
open(my $fh5, '<', $0); # Calls CORE::open
Output:
>perl test.pl
Hi
Ho
From there, I guess the idea is to make overrideautodie a lexical pragma. Shouldn't be too hard.
-
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.