----- Original Message ----- 
From: "Jaime Teng" <[EMAIL PROTECTED]>
To: <perl-win32-users@listserv.ActiveState.com>
Sent: Friday, March 03, 2006 11:10 AM
Subject: Need help with variables


> Hi,
>
> In my current web project, I group common functions and global variables
> into a separate file;
>
> my problem is, I could not seem to be able to access these variables
>
>
>
> ##############
> common.pm
> use strict;
> use DBI;
> my $HTTP;
> my $DBH = DBI->connect ( "DBI:mysql:database=sample; host=127.0.0.1",
> "root", "password");
>
> sub function1 {
> }
> sub function2 {
> }
> sub function3 {
> }
> 1
>
> ##############
> main.pl
> use common.pm
> my $sth = $DBH->prepare("......");
>
> I am getting:
> Global symbol "$DBH" requires explicit package name at main.pl line 2.
>
> I dont know how to make it work. I appreciate if you could lend me a hand.
>

One way - common.pm looks like:

use strict;
use warnings;
package common;
our $DBH = 17;
1;

The script looks like:

use warnings;
use strict;
use common;
print $common::DBH, "\n";
#And you access function1, etc. as:
# common::function1();

Another way - common.pm looks like:

use strict;
use warnings;
package common;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw($DBH);
#In your original example, this would become:
# our @EXPORT = qw($DBH function1 function2 function3);
our $DBH = 17;
1;

The script looks like:

use warnings;
use strict;
use common;
print $DBH, "\n";

I would think there are other solutions, too.

Cheers,
Rob


_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to