nedals has asked for the wisdom of the Perl Monks concerning the following question:
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();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using our $variables across modules
by chromatic (Archbishop) on Feb 13, 2005 at 21:45 UTC | |
by nedals (Deacon) on Feb 13, 2005 at 22:55 UTC | |
|
Re: Using our $variables across modules
by Animator (Hermit) on Feb 13, 2005 at 21:47 UTC | |
|
Re: Using our $variables across modules
by gaal (Parson) on Feb 14, 2005 at 06:02 UTC |