Created
February 7, 2013 15:11
-
-
Save rofr/4731511 to your computer and use it in GitHub Desktop.
LiveDomain Proxy with perl, step 1, intercept method calls and invoke on wrapped model.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package LiveDomain::Proxy; | |
sub new() | |
{ | |
my ($package,$system) = @_; | |
return bless {system => $system}; | |
} | |
sub AUTOLOAD | |
{ | |
our $AUTOLOAD; | |
(my $method = $AUTOLOAD) =~ s/.*:://s; #remove package name | |
my ($self,@args) = @_; | |
my $code = sprintf( '$self->{system}->%s(%s)', $method, join ", ", @args); | |
#TODO: log method and args | |
eval $code or die $@; | |
} | |
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use LiveDomain::Proxy; | |
use strict; | |
use warnings; | |
my $object = Model::create(); | |
$object->method(4..6); | |
print "Calls: ", $object->{calls}, "\n"; | |
my $proxy = new LiveDomain::Proxy($object); | |
$proxy->method(1,2,3); | |
print "Calls: ", $object->{calls}, "\n"; | |
package Model; | |
sub create | |
{ | |
return bless {calls => 0}; | |
} | |
sub method | |
{ | |
my ($self,@args) = @_; | |
$self->{calls}++; | |
print "Hello world!, args=[@args]\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment