If you want to get your hands dirty you can manipulate package main::'s symbol table. Below you'll see a number of examples of how to either manipulate your local symbol table, or access a function in another package without importing it or aliasing the symbols.

package AB; sub testing { print $_[0] . ": " . __PACKAGE__ . "\n"; } 1; package CD; sub testing { print $_[0] . ": " . __PACKAGE__ . "\n"; } 1; package main; use strict; use warnings; use 5.014; my $count = 0; foreach ( qw/ AB CD/ ) { no strict 'refs'; &{$_ . '::testing'}($count++); } *testing = \&AB::testing; # redefined warning testing($count++); *testing = \&CD::testing; # redefined warning testing($count++); $::{testing} = \&AB::testing; # redefined warning testing($count++); $main::{testing} = \&CD::testing; # redefined warning testing($count++); $main::{testing} = *AB::testing{CODE}; # redefined warning testing($count++); *testing = *CD::testing; testing($count++); *main::testing = *AB::testing; testing($count++); $main::{testing}= *CD::testing; testing($count++); foreach( qw/AB CD/ ) { no strict 'refs'; *main::testing = *{ $_ . "::testing" }; testing($count++); }

Not all of the above methods accomplish the same exact thing. Some alias an entire typeglob (an entire symbol). Others select only the subroutine for aliasing (leaving other "THING"s in the typeglob untouched. And some simply invoke the sub by fully qualifying its originally package name. I included a bunch of examples just to show some of the ways.

Note: Whenever Perl knows that you're specifically overwriting an existing declaration of a sub it will issue a warning. Typeglob to typeglob aliasing subverts that warning, but coderef to typeglob assignments do not subvert the warning. Consider it a feature, that Perl will complain to you when you do something silly if it sees you doing it.

One of the issues you face when using 'use' is that any subsequent 'use' that exports the same subroutine will mask the previous one. This is where carefully chosen exports is important, as well as knowing how to get at the fully qualified name.


Dave


In reply to Re: Recursive use statements? by davido
in thread Recursive use statements? by ellis

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.