I'm trying to figure out how to set a variable in the calling package that use's a package.

That question might not have been entirely clear, so let me provide an example. Consider the following three files:

--- dev.pl #!/usr/bin/perl -w use strict; use lib './'; use MyPackage; --- MyPackage.pm package MyBase; use strict; use Utils; # return true 1; --- Utils.pm package MyPackage; use strict; # export use base 'Exporter'; our @EXPORT = qw{mysub}; # import sub import { # what goes here to set $var in the caller package? # E.g. $MyPackage::var # export symbols MyPackage->export_to_level(1, @_); } # some subroutine sub mysub { # how do I access $var in the caller package? # E.g. $MyPackage::var } # return true 1;
Here's what I want to happen. When MyPackage uses Utils, Utils create a variable in MyPackage which can be accessed as $MyPackage::var. Furthermore, mysub() needs to be able to get to that variable when it's called. Of course, Utils doesn't know the name of the caller until, y'know, it's called, so I can't just hardcode in a package name.

I think I could muck out a solution using a lot of evals, but that seems like it would be very inefficient. Something a little more in keeping with how symbol tables work seems like a better solution.


In reply to 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.