DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Perl Function Proxying

11.05.2008
| 2954 views |
  • submit to reddit
        // This code is explained at http://awasihba.wordpress.com/2008/10/29/create-proxy-function-in-perl/



// function proxying module

package PROXY;
use strict;
use Exporter;
our @ISA    = qw(Exporter);
our @EXPORT = qw( Proxy );

sub Proxy {
  my $function_to_steal = shift;

  my $who_called = caller;
  my $function_name =
    $who_called . '::' . $function_to_steal;
  no strict;
  my $function_referance = *{$function_name}{CODE};

  *{$function_name} =
    sub { &Stub($function_referance,@_); };
}

sub Stub {
  my $original_function = shift;
  my @input = @_; 

  print "This is originating from stub function\n";
  $original_function->( @input );
}


// function proxying code using PROXY.pm

#!/usr/bin/perl
#
use strict;
use PROXY;

Proxy 'my_function';

sub my_function {
  my @input = @_;
  print "This is original my_function and inputs are ";
  print "@_\n";
}

&my_function ("one two three");