It is not a faux pas to access my variables globally, it's impossible (well, kind of). Global variables are attached to a namespace and that is how other packages access them. my variables are limited to the scope they are declared in.

Often, I'll establish my database connection that the beginning of a package and allow all subs in that package to use the lexically scoped DBI object. This works well if I have many places that use it.

Global variables tend to be more dangerous if they are shared between packages. This makes the packages interdependant. If you need to change a global in one package, you need to search through all other packages that reference this global variable and update them. This is a Bad Thing.

Of course, this also occurs when you have a my $var at the top of a particular package. If you have a bunch of subs that use it and you need to change the name, this could be problematic, but generally much less of an issue than if you're using a global across different packages.

Terminology alert: my variables declared in a package can only be accessed in that package. They are not globals and can only be accessed in the scope in which they were declared. Try the following (strict deliberately not used):

use Data::Dumper; $x = 1; print Dumper( \%:: );

By examining the output, you'll see that $x is in the main namespace. However, by putting a my in front of $x, it's removed from the namespace.

One thing I am unclear on, though: what mechanism does Perl use to access those variables that are lexically scoped and not in a namespace?

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid) Re: Top level by Ovid
in thread Top level my vars and their global use in a file by rchiav

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.