I'm in the process of modularizing a multi-script website. After reading thought some of the tutorials, I thought I understood variable scoping but maybe I'm missing something. In the code snippet below, I want to use global variables, declared in a module, in a second module but found that I needed to include the package name for each global variable.

This seems to work, but am I doing it right way?

## Header.pm package Header; use strict; use DBI; use Exporter; use vars qw(@ISA @EXPORT); @ISA = qw(Exporter); @EXPORT = qw($dbh $userID); our $dbh = DBI->connect('DBI:mysql:database'....); our $userID; 1; ## Some_subs.pm package Some_subs; use strict; use Exporter; use vars qw(@ISA @EXPORT); @ISA = qw(Exporter); @EXPORT = qw(get_data()); sub get_data { my $sql = "select * from table where id=$Header::$userID"; return $Header::dbh->selectrow_array($sql); } 1; ## main ## use strict; use Header; use Some_subs; $userID = 1; my $data = &get_data();
update: Hyphen to underscore. Thanks chromatic
Also corrected the exported variable list

In reply to Using our $variables across modules by nedals

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.