Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!male!texsun!convex!newsadm From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: package switching Message-ID: <1991May01.170306.891@convex.com> Date: 1 May 91 17:03:06 GMT References: Sender: newsadm@convex.com (news access account) Reply-To: tchrist@convex.COM (Tom Christiansen) Distribution: comp Organization: CONVEX Software Development, Richardson, TX Lines: 51 Nntp-Posting-Host: pixel.convex.com From the keyboard of dnb@meshugge.media.mit.edu (David N. Blank): :Howdy- : I've been having a great deal of fun with eval lately (as Tom and :Randall both know) in an effort to make something I am writing really :generic. I've hit another stumper on the subject: "package". If I :want to be able to switch packages in a very generic way: : :sub blah{ : package @_[0]; :&blah("bleh"); If that were to work, you would really mean $_[0] -- don't want to be supplying array contexts willy-nilly. :How do I do it? I have seen Randall use this idiom: : :> $ret = eval "package main; $action" : :but I would prefer not to stick the entire contents of a subroutine :into one scalar variable. Is there some magic way to expand the :scope of the package commands? Thanks. Magic way? Nay. A package declaration only affects what default package identifiers get compiled into. That means it doesn't affect the run time scoping at all,unlike local(). Thus simply doing package main; eval "package $package"; $foo = 1; won't do you any good, as you're not compiling anything: foo will still be in main, not $package. You've little choice but to put your subroutine in a variable an eval it so it gets compiled. if that's what you're trying to do. This is expensive. Now, the only time I really care about other things' packages is when I get a filehandle passed into me as a variable. For example, in my open2() routine in the FAQ, I do this: package open2; # blah blah # force unqualified filehandles into callers' package local($package) = caller; $dad_rdr =~ s/^[^']+$/$package'$&/; $dad_wtr =~ s/^[^']+$/$package'$&/; Other than that, I'm not sure why you want to dynamically switch packages at run time instead of compile time. --tom