in reply to Re: Defining a function in the caller's package
in thread Defining a function in the caller's package

Don't make a closure. Just assign the typeglob.

This would not solve my problem (sorry if I was not precise enough about this). If I would adapt your example for my application, it would look more like:

package ForeignModule; sub NEXT { print scalar caller } ================================= package Foo; use ForeignModule qw(NEXT); sub do_something { NEXT(); } sub import { my $target = caller; no strict 'refs'; *{"$target:\:do_something"} = \&do_something; } ================================= package main; use Foo; do_something();
This would print 'Foo', but I would like to have it printed 'main'. This can be accomplished using, for instance, the eval "package ...." mechanism.

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re^3: Defining a function in the caller's package
by mr_mischief (Monsignor) on Aug 11, 2008 at 16:10 UTC
    Don't do that. You're assigning a sub in Foo to return the results of a call made to ForeignModule. Then you're setting main::do_something to be the same as that subroutine. What you actually want is not to return the value but to make main::do_something the very same sub as ForeignModule::NEXT.
    package ForeignModule; sub NEXT { print scalar caller } 1;
    package Foo; # there is no do_something sub import { my $target = caller; no strict 'refs'; *{"$target:\:do_something"} = \&ForeignModule::NEXT; } 1;
    package main; use strict; use warnings; use Foo; do_something();
      Thanks a lot for the clarification!