in reply to Set a variable in calling package
Here's another minimal example. It passes a module name and a string to a parent module that then handles everything. Please understand that I do not recommend this code as any kind of example of best practice or even as vaguely kosher.
# MyTools.pm package MyTools; use warnings; use strict; sub import { my ($importing_package, $string_for_export, ) = @_; $string_for_export = '[default]' if ! defined $string_for_export; { no strict 'refs'; # turn off in narrowest scope # use a symbolic reference (gasp) ${ $importing_package . '::var' } = $string_for_export; } } 1;
# MyChest.pm package MyChest; use warnings; use strict; use parent qw(MyTools); 1;
# MyBag.pm package MyBag; use warnings; use strict; use parent qw(MyTools); 1;
Script set_in_importing_script_2.pl:# MyBox.pm package MyBox; use warnings; use strict; use parent qw(MyTools); 1;
use warnings; use strict; use MyChest 'Swiss Army knife'; use MyBox '6-bladed screwdriver'; use MyBag; # no string specified print 'all variables printed from ', __PACKAGE__, ": \n"; print " var in MyChest: '$MyChest::var' \n"; print " var in MyBox: '$MyBox::var' \n"; print " var in MyBag: '$MyBag::var' \n";
c:\@Work\Perl\monks\TerryBerry>perl set_in_importing_script_2.pl all variables printed from main: var in MyChest: 'Swiss Army knife' var in MyBox: '6-bladed screwdriver' var in MyBag: '[default]'
Update: Slight wording change in intro to improve clarity (I hope). No code change.
Give a man a fish: <%-{-{-{-<
|
|---|