No offense but mass importing of tons of variables is a really really bad idea. If you want to use globals, why not just give them their own short package name like so.
package CFG;
So at least looking at your source 3 months later it's clear what the hell $CFG:this is and you won't step on any variables in the scripts or vice/versa unintentionally.
But to give you enough rope to hang yourself, you could do something like this.
package other;
$VAR = 'SCALAR';
@array = ('ARRAY') x 4;
my @exports = qw ($VAR @array);
sub import {
no strict 'refs';
my $used_name = shift; # Remove the use name
my $that_pack = caller();
foreach (@exports){
m/^([\@\$\*\&\%])(\w+)/;
my ($t,$v) = ($1,$2);
# Selectively assign to typeglobs.
*{$that_pack.'::'.$v} = $t eq '@' && \@{$v} || $t eq '%' && \%
+{$v} ||
$t eq '&' && \&{$v} || $t eq '$' && \$
+{$v} ||
$t eq '*' && \*{$v};
}
}
package main;
other::import();
foreach (sort keys %main::){
print __PACKAGE__,"\:\:$_ = $main::{$_}\n";
}
warn "\$VAR is $VAR";
warn join(", ",@array);
Update:
Fixed typo in code.
Update:Fixed
Ran into another snag. Seemed to work on win but no nix.
Fixed. If anyone knows and easier way. Let me know.
If you want to use this, use maintained code HERE
-Lee
"To be civilized is to deny one's nature."
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.