Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-24 06:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found