Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Using delete package::{subroutine} to change resolution of subroutines

by ikegami (Patriarch)
on Jun 22, 2008 at 07:30 UTC ( [id://693347]=note: print w/replies, xml ) Need Help??


in reply to Using delete package::{subroutine} to change resolution of subroutines

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.

Replies are listed 'Best First'.
Re^2: Using delete package::{subroutine} to change resolution of subroutines
by pjf (Curate) on Jun 22, 2008 at 08:40 UTC

    ikegami, I cannot begin to express just how happy you've made me. Having simple, elegant, and understandable code that solves a problem that I've been beating my head against for days is stunningly good.

    You've been added with an appropriate attribution to the AUTHORS file, and you can expect a future release of autodie to be named in your honour. ;)

    Really, you rock. Thank-you so much for taking the time to show me a solution, it's exactly what I've been trying (and failing) to do.

    Yours most humbly,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://693347]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-06-13 01:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.