Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: How to alias a whole namespace to another namespace (in this case main::)

by !1 (Hermit)
on Nov 15, 2004 at 16:00 UTC ( [id://407847]=note: print w/replies, xml ) Need Help??


in reply to How to alias a whole namespace to another namespace (in this case main::)

I had to do this some time back and ended up with this:

package as; sub import { my ($class,$caller) = ($_[1],caller); die "Mal-formed package name '$class'" unless $class =~ /^(?:\w|(? +<!^)(?<!:)::(?!\z))+\z/; die "Namespace already exists" if (keys %{"$class\::"}); *{"$class\::"} = \*{"$caller\::"}; ($class .= ".pm") =~ s!::!/!g; ($caller .= ".pm") =~ s!::!/!g; $INC{$class} = exists $INC{$caller} ? $INC{$caller}:$0; 1; } 1;

Update: Pfft, I went the wrong way. You're going to want to alias the main namespace to your target namespace as such:

# here we are in Utils.pm package main; use as "Utils"; package Utils; ...

The reason for this is that you cannot reassign the main:: namespace. This merely makes Utils point to main's namespace and anything declared in Utils is also being declared in main.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://407847]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-03-29 10:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found