in reply to Re^2: RFC Bridge::Simple
in thread RFC Bridge::Simple

You have to keep the name space of a bridge clear, so I would inline the curry_method function (alternatively, you could keep it in some other namespace). However, here's a simple case demonstrating that slurping @_ works:
$ perl -MCGI -e 'sub curry_method{ my ($o, $m) = @_; sub { $o->$m(@_) } } $c = CGI->new; $p = curry_method($c, 'p'); print $p->("Hello World"), "\n"' <p>Hello World</p>
You make a good point about having a closure for every method of every instance of the bridge class. Its a little extra space for a little extra simplicity. I prefer simplicity to speed/space in this case.

The doubled $obj in @_ only occurs if you are calling the thing like a method twice. Once the object is curried, the method becomes a plain old function. I envisioned something like this (untested):
package Bridge::Simple; use strict; use Carp; sub new { my ($class, $obj, $mapping) = @_; my $self = {}; @{$self}{keys %$mapping} = map { my $meth = $_; sub { $obj->$meth(@_) } } values %$mapping; bless $self, $class; } sub AUTOLOAD { my $self = shift; our $AUTOLOAD =~ s/.*:://; croak "no method $AUTOLOAD" unless exists $self->{$AUTOLOAD}; goto $self->{$AUTOLOAD}; } 1;