Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: A general method of locally overriding subroutines

by Arunbear (Prior)
on Mar 11, 2006 at 13:40 UTC ( [id://535941]=note: print w/replies, xml ) Need Help??


in reply to A general method of locally overriding subroutines

There is a module that provides this facility:
use Sub::Override; use strict; use warnings; $, = ' '; $\ = "\n"; sub a{ 'a' } sub b{ 'b' } sub c{ 'c' } sub d{ ( a, b, c ) }; print d(); { my $override = Sub::Override->new; $override->replace(a => sub { "changed" }) ->replace(b => sub { "changed" }) ->replace(c => sub { "changed" }); print d(); } print d();
output:
a b c changed changed changed a b c Tool completed successfully

Replies are listed 'Best First'.
Re^2: A general method of locally overriding subroutines
by xdg (Monsignor) on Mar 11, 2006 at 14:08 UTC

    That's a really neat way to do it. I used a similar technique for File::pushd -- using an object to enact a localized change that is reverted when the object goes out of scope.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      Isn't it nice to have deterministic behavior that fires when an object goes out of scope? I miss that when working in java.
      --
      @/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (1)
As of 2024-04-25 01:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found