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.


In reply to Re: How to alias a whole namespace to another namespace (in this case main::) by !1
in thread How to alias a whole namespace to another namespace (in this case main::) by aufflick

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.