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;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.