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;
# MyBox.pm package MyBox; use warnings; use strict; use parent qw(MyTools); 1;
Script set_in_importing_script_2.pl:
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";
Output from set_in_importing_script_2.pl:
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:  <%-{-{-{-<


In reply to Re: Set a variable in calling package by AnomalousMonk
in thread Set a variable in calling package by TerryBerry

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.