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.


In reply to Re^2: "importing" methods? by tobyink
in thread "importing" methods? by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.