This works. It actually works better without curry because then you don't need $object to be defined yet at compile-time.
use 5.018;
use strict;
use warnings;
BEGIN {
package Foo;
use Moo;
sub meth {
die unless ref shift;
join "|", @_;
}
sub othermeth {
die unless ref shift;
join ":", @_;
}
$INC{'Foo.pm'} = __FILE__;
};
BEGIN {
package curry::lexical;
use Exporter::Lexical ();
use Exporter::Tiny ();
sub import {
my $me = shift;
my $objref = shift;
my $optlist = Exporter::Tiny::mkopt(\@_);
for my $method (@$optlist) {
my ($name, $currystuff) = @$method;
my ($originalname, @args) = ref($currystuff) ? @$currystuf
+f : $name;
my $coderef = sub { $$objref->$originalname(@args, @_) };
Exporter::Lexical::lexical_import($name, $coderef);
}
}
$INC{'curry/lexical.pm'} = __FILE__;
};
use Test::More;
sub meth { "FUNCTION" }
is meth("foo"), "FUNCTION";
{
my $obj = Foo->new;
use curry::lexical \$obj, "meth", "meth2" => ["meth", "prefix"], "
+othermeth";
is meth("foo"), "FUNCTION", "non-lexical sub wins";
is meth2("foo"), "prefix|foo";
is othermeth("foo", "bar"), "foo:bar";
}
is meth("foo"), "FUNCTION";
done_testing;
So with your example, you could do something like:
my $app = MyApp->new;
{
use curry::lexical \$app, qw(log foo bar), critical_log => [log => "
+CRITICAL"];
log("Hi"); # $app->log("Hi");
foo(); # $app->foo();
bar(123); # $app->bar(123);
critical_log(); # $app->log("CRITICAL");
}
The functions imported via curry::lexical cannot overwrite "normal" functions, as shown in the second test of the test script.
|