Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Here is something to think about, I 'm not even sure it's possible.
>>>>>>>>>>>>>>>>> package first ; sub first_one {print "1\n" ; first_two()} ; sub first_two {print "2\n" ; print join(':', caller()) . "\n"} ; our @x ; package second ; $import_from = 'first' ; for $symbole_name (keys %{"$import_from\::"}) { local *symbole = "$import_from\::"->{$symbole_name} ; print "$symbole_name " ; if(defined *symbole{CODE}) { print "is code" ; *{__PACKAGE__ . "\::$symbole_name"} = \&{"$import_from\::$symbol +e_name"} ; } print "\n" ; } first_one() ; <<<<<<<<<<<<<<<<<<<<<<<<<<<
This prints:
first_two is code
x
first_one is code
1
2
first:/home/nadim/Desktop/test.pl:4
a little explaination, I declare 2 subs in a package then I alias them into another package. I'd be glad to see how you write it if you have a better syntax. sub first_two prints from which package it's called. That is invariably package 'first'. The sub resides in its declaration package and even though the alias is in another package the call is done in the original package. I have no particular problem with that except that I would have find it nicer (and right) to have the call made in the aliasing package. My questions are: Is there a way to force a function call (an alias) into a package?
SetPackageAndCall("ihha", \&first_one, arg1, arg2)
Is there a way to modify the package while inside the sub? Fiddling with the stack? overriding 'caller'?
>>>>>>>>>> package first ; sub first_one { my $package = shift ; SetMyPackage($package) ; first_two() ; } sub first_two { sub first_two {print "2\n" ; print join(':', caller()) . "\n"} ; } package second ; # import the subs as before first_one('a_completely_diffrent_package') ; <<<<<<<<<<<<<< the above would print: a_completely_diffrent_package:/home/nadim/Desktop/test.pl:4 I'm all ears, Nadim. PS. there is a practical use that I'll gladely if someone is interrested.

edited: Thu Jun 10 21:05:22 2004 by jeffa - formatting

Replies are listed 'Best First'.
Re: Playing with package spaces
by BUU (Prior) on Jun 10, 2004 at 22:13 UTC
    PS. there is a practical use that I'll gladely if someone is interrested.
    Please do. I'm sure I'm not the only one wondering what exactly you're trying to accomplish here.
      I am trying to have an aliased sub run in a diffrent package. That was the question.

      Now about the practical use, I fiddle a lot with packages to allow users to write scripts (in my build system) without having to think about the details. In this case, we avoid using an object oriented syntax. The build system pushes the user scripts in a package (dynamicaly computed). wehen multiple scripts are run simulteanously, the rules defined in each script gets assigned to a particular package so nothing gets mixed. This sytem works very good but has the disadvantage of being a bit slow. I was thinking of caching the packages and aliasing them instead for re-loading them but the aliased functions must be called in the aliasing package. Since I got that idea, I found out that this caching system doesn't bring much speed but I still am curious about how to fiddle with packages when it comes to alised subs.

      A description of the build system can be found on the "perl nordic workshop" web site. If anyone is interrested in a build system written in perl and scriptable with perl, you can have a look at the slides then contact me.

      Cheers, Nadim.