This follow up is just an example, and to be honest, I've put it here for thoroughness... it'll break in many cases, so don't use it.

You can exploit the symbol table to slurp in subs from modules that have imported them into their own namespaces. Know that "namespaces" are important in Perl (and in any other language that deals with them) due to logical (ie. lexical) scoping. The following will show how to check a 'use'd module for subs itself has, and import them all into the current namespace. Again, this is bound to break (I haven't even considered ramifications on included modules that use OO, and not using strict in the way I have here could be catastrophic (if it isn't already ;) ).

Rephrasing... it'll import *all* subroutines that's in the module's namespace as it currently sits, which is almost certainly not what you want. Look at it as indiscriminate inclusion.

First, the all-inclusive module you want to pull from (X.pm):

package X; use Data::Dumper; use File::Copy qw(copy); use Time::HiRes qw(usleep); 1;

Now, the x.pl script that wants everything from the module:

use warnings; use strict; use lib '.'; use X; BEGIN { no strict 'refs'; for (keys %X::){ my $symbol = "X::$_"; if (defined &{$symbol}){ print "importing '$symbol' sub into our namespace\n"; *$_ = \&{$symbol}; } } } print Dumper {a => 1}; usleep 500000; copy 'x.txt', 'y.txt' or die $!;

Output:

importing 'X::copy' sub into our own namespace importing 'X::usleep' sub into our own namespace importing 'X::Dumper' sub into our own namespace $VAR1 = { 'a' => 1 }; No such file or directory at x.pl line 20.

Please don't do this unless you know exactly why you're doing it, what you're doing it for, and with knowledge exactly why you want to 'cheat'. The other Monks have put forth much better examples of how to accomplish what you really want in much more eloquent ways.

This could be relatively trivially adapted so that the module itself saturates the caller script's namespace with the subs, but because this is not a recommended technique unless required for very specific purposes, I'll leave it up to the reader to figure that piece out.


In reply to Re: Code Reuse by stevieb
in thread Code Reuse by Mano_Man

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.