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?
update: Hyphen to underscore. Thanks chromatic## 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();
In reply to Using our $variables across modules by nedals
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |