Dear fellow monks,

I would like to know if there is a way of accessing a scalar defined in a program from a module that this program uses.

Let's imagine, I have a program:

#!/usr/bin/perl -w use strict; use My::Module; my $foo = "some string" my $bar = Somefunction();

and a module

package My::Module; our @EXPORT = qw/Somefunction/; use Exporter; our @ISA = qw/Exporter/; sub Somefunction { # do something; }

I want to use the information contained in $foo in the module. I know, I could pass $foo as an argument, but I have a lot of functions in my module and I would like to avoid this redundant information.

I have already tried things like $main::foo, but it does not work (maybe I am missing something, or it might even be a completely stupid idea, I must say I have no clue).

Could someone please just tell me if there is a smart way of accessing a variable in a subroutine (the shorter the better).

Thanks in advance,

Update: Change My/Module to My::Module

--
jey

In reply to Accessing a Scalar from a module by jey

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.