I have a script (we'll call it test.pl), which "use"s a module (we'll call it my_module.pm).

test.pl is kicked off with some varying arguments provided to it (i.e. different arguments each time it's run), and I would like to share one of these variables with my_module.pm, so that when my_module.pm first gets loaded it'll do something different according to the variable's value.

So, examples of what I have are below:

package my_module; $VERSION = '1.00'; use strict; our $gMessage; sub import { $gMessage = @_; print "Yep, I'm in here ... \n\n"; } &my_sub(); sub my_sub { print $gMessage; }

test.pl looks like this:

#!/usr/bin/perl use strict; use my_module 'xyz'; ... ... ...

In this test.pl, just for an example, I'm just providing a static argument, but in reality it's a varying value.

Nevertheless, what I'm anticipating is that test.pl will "use" my_module, and pass in "xyz" and then the module will print the "Yep, I'm in here" message, followed by "xyz" when it gets to my_sub, which is called when my_module is loaded.

However, I guess I must be misunderstanding something because the "Yep, I'm in here" message never gets printed, and the variable is empty when it gets to my_sub.

Thus, I guess the import sub is never being called, but I thought that by default an import sub is always called, if it exists, when "use"ing a module.

Noting that I don't want to share a variable from the module to the script, but vice-versa, if anyone could shed some light, or suggest a better method, then I'd be grateful. Thanks!


In reply to Share Variable in Script with a Module by DanielSpaniel

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.