This prints:>>>>>>>>>>>>>>>>> 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() ; <<<<<<<<<<<<<<<<<<<<<<<<<<<
first_two is code x first_one is code 1 2 first:/home/nadim/Desktop/test.pl:4a 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?
Is there a way to modify the package while inside the sub? Fiddling with the stack? overriding 'caller'?SetPackageAndCall("ihha", \&first_one, arg1, arg2)
>>>>>>>>>> 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
In reply to Playing with package spaces by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |