This idea came from my (mis?)understanding of something in the CB, and is probably a bad idea (seeing as how this ought to be done through the Exporter module or whatever export mechanism is built into a module), but I figured as long as its doable, and as long as there's an Exporter module, there ought to be an Importer module which will import variables and subroutines from other modules.

Update: A possible valid use for this is in the case of a module which overuses the @EXPORT array, and you don't want to import everything it exports, so you require the module, and use this Importer module to import only the things you want. Another update: No, then you'd just include the symbols you want in the import list of the 'use' statement when you use a module. So I'm still looking for a valid use for this :-)

package Importer; sub import { shift; for (@_) { if (/^([\$@&%]?)(?:\w+::)*(\w+)$/) { ($sigil, $name) = ($1, $2); $var = $sigil ? "\\$_" : "*$_"; eval "*" . caller() . "::$name = $var"; die $@ if $@; } else { die "Can't import $_" } } } 1; # Example use strict; use warnings; use Importer qw($MyPackage::var); # Pretend you have a module/package "MyPackage" that # contains a package variable "var" $MyPackage::var = 5; print "$var\n"; # Update: Alternate version which avoids string eval package Importer; my %sig = ( '$' => sub { *{"$_[0]::$_[1]"} = \${$_[2]} }, '@' => sub { *{"$_[0]::$_[1]"} = \@{$_[2]} }, '%' => sub { *{"$_[0]::$_[1]"} = \%{$_[2]} }, '&' => sub { *{"$_[0]::$_[1]"} = \&{$_[2]} }, '*' => sub { *{"$_[0]::$_[1]"} = *{$_[2]} }, ); # Yet another update: Or maybe you like this better: my %sig = map { $_ => eval sprintf 'sub { *{"$_[0]::$_[1]"} = %s{$_[2]} }', ($_ eq "*") ? $_ : "\\$_" } qw($ @ % & *); sub import { shift; for (@_) { if (/^([\$@&%*]?)((?:\w+::)*(\w+))$/) { ($sigil, $name, $base) = ($1 || "*", $2, $3); $sig{$sigil}->(scalar(caller), $base, $name); } else { die "Can't import $_\n" } } } 1;

In reply to Importer by runrig

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.