It's hard to know for certain what the right approach is without knowing more about what you're doing. However, based on the information you've given, it sounds like an import method would probably be overkill. If you have just a single value to pass in, the approach I'd take is probably to provide a package method to set the variable; this can return its value if you want the outside world to see it. Here's a trivial example module:

package Foobar; my $var; # If called with no arguments, just returns the value. Otherwise sets +and returns. sub foovar { $var = shift if @_; return $var; } # This version won't allow the value to be set twice sub foovar_once { die "Attempt to foovar more than once\n" if @_ && defined($var); $var = shift if @_; return $var; } 1;
And here's a test script:
use Foobar; if (1) { Foobar::foovar(7); print "1. The value is now ", Foobar::foovar(), "\n"; Foobar::foovar(9); print "2. The value is now ", Foobar::foovar(), "\n"; } else { Foobar::foovar_once(7); print "1. The value is now ", Foobar::foovar_once(), "\n"; Foobar::foovar_once(9); print "2. The value is now ", Foobar::foovar_once(), "\n"; }

Change the 1 to a 0 in the if test to see the other behavior.

HTH,
--roundboy


In reply to Re: Re: Packages and modules by roundboy
in thread Packages and modules by Anonymous Monk

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.