Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global symbol requires package

Status
Not open for further replies.

RexJacobus

Programmer
Dec 10, 2001
47
NZ
The beginning of all our scripts look like this


use Mysql;
use Debug;
use Socket;
use strict;
require "include.pl";
require "tlib/subs.pl";
require "tlib/gs_db.pl";
require "tlib/common.pl";


inside gs_db.pl

i have the code


sub connect()
{ my ($script)=@_;
my $db_type = $'db_type;
my $database= $'database;
my $db_host = $'db_host;
if($script!="") {$'parent_script=$script; }
my $dbh = DBI->connect("DBI:${db_type}:${database}:${db_host}",$'db_user,$'db_password);
if(!$dbh)
{ &logError("Trying to connect","db: ${database}\@${db_host} user:$'db_user");
}
return($dbh);
}


But I am still getting the error
Global symbol "$dbh" requires explicit package name at C:/...

I understand use Strict means that $dbh must be declared but whu doesn't it accept the declaration in gs_db.pl?

I am trying to set up a dev environment on a brand new PC and all the scripts are fine and run elsewhere.

(I hate installing stuff, just let me program.)

jim
 
Post the entire error message, where it gives a line number and the name of the script.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
ENTIRE ERROR MSG

Global symbol "$drh" requires explicit package name at C:/.../cgi-bin/ConnectTest3.cgi line 23.
Global symbol "$dsn" requires explicit package name at C:/.../cgi-bin/ConnectTest3.cgi line 24.
Global symbol "$dbh" requires explicit package name at C:/.../cgi-bin/ConnectTest3.cgi line 25.
Global symbol "$dsn" requires explicit package name at C:/.../cgi-bin/ConnectTest3.cgi line 25.
Global symbol "$dbh" requires explicit package name at C:/.../cgi-bin/ConnectTest3.cgi line 29.
Global symbol "$dbh" requires explicit package name at C:/.../cgi-bin/ConnectTest3.cgi line 39.
Global symbol "$dbh" requires explicit package name at C:/.../cgi-bin/ConnectTest3.cgi line 62.
 
the culprit appears to be ConnectTest3.cgi not gs_db.pl. Can we see ConnectTest3.cgi?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Here is the code from ConnectTest3.cgi

-------------

#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "**********Connect Test3\n";

use DBI;
use CGI::Carp qw( fatalsToBrowser );
use CGI qw:)standard);
use Mysql;
use Debug;
use Socket;
use strict;
use CGI qw:)standard escapeHTML);

require "include.pl";
require "tlib/subs.pl";
require "tlib/gs_db.pl";
require "tlib/common.pl";


# Connecting to the database
# Replace DATABASENAME with the name of the database,
# HOSTNAME with the hostname/ip address of the MySQL server.
$drh = DBI->install_driver("mysql");
$dsn = "DBI:mysql:database=tantrix;host=localhost";
$dbh = DBI->connect($dsn,"USER","PWD");

# Select the data and display to the browser

my $sth = $dbh->prepare("SELECT * FROM players where player_name = 'RexJacobus'");
$sth->execute();
while (my $ref = $sth->fetchrow_hashref()) {
print "Found a row: id = $ref->{'ranking'}, name = $ref->{'full_name'}n";
}

$sth->finish();

# Disconnect from the database.

$dbh->disconnect();

_______________

jim

 
it appears as though these three lines need the left side scalar declared with "my":


$drh = DBI->install_driver("mysql");
$dsn = "DBI:mysql:database=tantrix;host=localhost";
$dbh = DBI->connect($dsn,"USER","PWD");


my $drh = DBI->install_driver("mysql");
my $dsn = "DBI:mysql:database=tantrix;host=localhost";
my $dbh = DBI->connect($dsn,"USER","PWD");

see if that helps.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I'm trying to avoid that. At the moment all my scripts work on the server and on the other dev PC. But they refuse to work on the new PC which has new installs of Perl and Apache.

I'm just looking to be pointed in the right direction as to what in the new install must be wrong.

jim

 
If the above code works on the other server and those variables are not declared with "my", something is wrong with the other server I would think. I doubt there is anything wrong with the new installation, just declare the variables and your script should hopefully work. That is the way it is supposed to be coded, you can see each undeclared varibable is generating an error:

Global symbol "$drh" requires explicit package name at C:/.../cgi-bin/ConnectTest3.cgi line 23.
Global symbol "$dsn" requires explicit package name at C:/.../cgi-bin/ConnectTest3.cgi line 24.
Global symbol "$dbh" requires explicit package name at C:/.../cgi-bin/ConnectTest3.cgi line 25.

OBSOLETE SOFTWARE ^

As of Msql-Mysql-modules 1.19_10 M(y)sqlPerl is no longer a separate module. Instead it is emulated using the DBI drivers. You are strongly encouraged to implement new code with DBI directly. See "COMPATIBILITY NOTES" below.

quote from:
Your code looks quite old and that might be adding to your problems. Besides that maybe someone else will have a suggestion but there is no way to know what could be wrong with an entire installation of an unknown version perl on an unknown operation system on an unknown architecture, and even if I knew those things it is way out of my range of knowledge and experience to be of any help in such a broad and complex subject.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Must admit, I can't see why it would work on the other server. The script has use strict; coded, so I would expect it to fail without declaring the three variables with my.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I beleive the OP is thinking since it is declared with "my" in the module the main program should run.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top