The problem you are having is with scope. Since you are making $one a local variable, it is only available withing the main package and the main code. So when another package accesses $main::one, it is actually getting the global version of the variable, which is undefined. At least this is how it was explained to me. If someone knows better, be sure to tell us!!!!

To get you code to work, you can do one of two thing.
1. Make $one a global. I don't like this idea, but it is there.
2. Use a typeglob so the variable is in scope. This is a much cleaner way to deal with the problem. To use a typeglob, you would change your code to look like this:

package Shop; use strict; use Exporter; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT = (); @EXPORT_OK = qw(db_connect); sub db_connect { *one = *main::one; print "Print - ",$one, " - from the Shop package\n"; } 1;
P.S. Why are you accessing vars in main anyway? You may want to reconsider your design to use an object and have your main script set parameters on that object. Or you should have the variable be in your package, and then export or typeglob it to main's name space.

In reply to Re: using main package variables within a module by SarahM
in thread using main package variables within a module by fireartist

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.