In the node Inheriting constants in the same file -- help me find a better way! I asked about a better way to share constants within the same file. After some work, I came up with this package that you may find useful.

It lets you import all the subroutines (excluding a few dangerous ones) from one or more other packages. Using it is as simple as:

package B; use AllSubsFrom 'A';

which will import all the subs (including constants) from package A into package B.

# AllSubsFrom.pm - Perl module to allow importing of all subroutines f +rom # another package or packages. # # By Ned Konz, perl@bike-nomad.com # # Usage: # # package X; # use AllSubsFrom qw(A B C); # # can also be called explicitly on any package: # # MyPackage->importAllSubsFrom('SomeOtherPackage'); # # MyPackage->importAllSubsFrom qw(SomeOtherPackage YetAnotherPackag +e); # use strict; package AllSubsFrom; $AllSubsFrom::VERSION = '0.1'; # calls AllSubsFrom::import('A', 'B', 'C'); sub import { UNIVERSAL::importAllSubsFrom( caller, @_ ); } # This is in UNIVERSAL, the root of all evil. # That way, it can be called as a class method on any package. # Export all the not-already-defined subroutines from one # package to another package: # To->importAllSubsFrom('From1', 'From2'); # importAllSubsFrom('To', 'From1', 'From2'); sub UNIVERSAL::importAllSubsFrom { my $to = shift; foreach my $from (@_) { no strict 'refs'; # to put it mildly. my $fromSymtab = \%{ 'main::' . $from . '::' }; foreach my $key ( keys %$fromSymtab ) { # avoid dangerous subs next if $key =~ m{^(?:BEGIN|END|INIT|CHECK|AUTOLOAD|DESTRO +Y|__.*)$}; my $codeRef = *{ $from . '::' . $key }{CODE}; next if !defined($codeRef) # symbol b +ut no sub || defined( *{ $to . '::' . $key }{CODE} ); # don't cl +obber dest *{ $to . '::' . $key } = $codeRef; # replace just the C +ODE ref } } } # This imports all the subs to the caller's package # From->exportAllSubsToMe; sub UNIVERSAL::exportAllSubsToMe { UNIVERSAL::importAllSubsFrom( caller, shift ); } "now wasn't that an exciting trip?";

In reply to Import all subroutines from another package whether it wants to export them or not! by bikeNomad

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.