in reply to Defining a function in the caller's package

You can change the package locally...

*{"$target\::nx"} = do { package soemthing; sub { do_something() } } ... The problem seems to be that your anonymous sub remembers where it was first compiled.

I'd recommend doing this job with Exporter though. Is there some reason that doesn't fit? (Probably because it would use the master namespace, now that I think about it.)

-Paul

Replies are listed 'Best First'.
Re^2: Defining a function in the caller's package
by rovf (Priest) on Aug 08, 2008 at 11:57 UTC
    Is there some reason that doesn't fit? (Probably because it would use the master namespace, now that I think about it.)

    Exactly for this reason. BTW, I like your suggestion using do. Clearer than my eval and, maybe, also clearer than messing around with __ANON__.

    -- 
    Ronald Fischer <ynnor@mm.st>
Re^2: Defining a function in the caller's package
by rovf (Priest) on Aug 08, 2008 at 12:01 UTC

    On second thought, your solution does not work for me, sadly. The problem is that I don't know the package name beforehand. Hence I would have to write something like:

    .... do { package $target; ... }
    but, as I tried out right now, you can't pass variables to the package statement. Hence I'll have to stick with eval.

    -- 
    Ronald Fischer <ynnor@mm.st>
      Yeah, I noticed that. package should take expression arguments imo... You're pretty much forced to use eval because of that. I imagine there's some trick to set the package using symbol table magic, but it's probably not recommended.

      -Paul