Why would you want to do that? False laziness?

If your package relies on the availability of a global found in the main program (which would be $main::var, as already mentioned), then you will be unable to use it as a part of any program which does not contain that same variable and make it available as a global under the same name. In your example of what you (think you) want, using Mypackage in any program which does not provide a global $main::var will mean that you then have to remember to never call printthis.

Packages should not impose requirements of this sort on the code which uses them.

In most cases, the correct approach for sharing variables between a package and other code would be to define it in the package and export it to the code using the package. But that may not be well-suited to what you're actually attempting to accomplish. So what are you actually attempting to accomplish, so we can advise on the best way to do it?


In reply to Re: Access variable from inside a package? by dsheroh
in thread Access variable from inside a package? by tamaguchi

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.