For Programmers: Free Programming Magazines  


Home > Archive > PERL Modules > July 2004 > error: requires explicit package name useing DBI









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author error: requires explicit package name useing DBI
Andy Stevenson

2004-07-14, 3:55 am

Hi,

I can't find the answer to this one, I'm trying to access an MS access
database from a web page.

I'm following the article at :
http://www.databasejournal.com/feat...10895_1408481_1
and I've got stuck trying to run the cgi script. I get this result.


Global symbol "$dbh" requires explicit package name at
c:\INDIGO~1\cgi-bin\access.cgi line 10.
Global symbol "$sqlstatement" requires explicit package name at
c:\INDIGO~1\cgi-bin\access.cgi line 13.
Global symbol "$sth" requires explicit package name at
c:\INDIGO~1\cgi-bin\access.cgi line 14.
Global symbol "$dbh" requires explicit package name at
c:\INDIGO~1\cgi-bin\access.cgi line 14.
Global symbol "$sqlstatement" requires explicit package name at
c:\INDIGO~1\cgi-bin\access.cgi line 14.
Global symbol "$sth" requires explicit package name at
c:\INDIGO~1\cgi-bin\access.cgi line 15.
Global symbol "@row" requires explicit package name at
c:\INDIGO~1\cgi-bin\access.cgi line 19.
Global symbol "$sth" requires explicit package name at
c:\INDIGO~1\cgi-bin\access.cgi line 19.
Global symbol "@row" requires explicit package name at
c:\INDIGO~1\cgi-bin\access.cgi line 20.
Execution of c:\INDIGO~1\cgi-bin\access.cgi aborted due to compilation
errors.

This would suggest it's a problem with the perl module, but this being my
first dabble with installing modules I'm not sure :)

The server is Win XP running apache 1.2.26 & the GUI package manager tells
me I've got DBI-1.37 & DBD-ODBC-1.06 installed. I'm running other cgi
scripts ok, take a look: http://www.stevenson-central.no-ip.com/.

The access DB is sitting in a directory on the server & I've set ODBC as
outlined in the article.

Any suggestions greatly appreciated.

TIA.


Sherm Pendley

2004-07-14, 3:57 pm

Andy Stevenson wrote:

> Global symbol "$dbh" requires explicit package name at
> c:\INDIGO~1\cgi-bin\access.cgi line 10.


Looks like you're using 'strict' - which is a *very* good idea - but you're
trying to copy code from a badly-written tutorial that didn't bother using
it. You should try to find a better tutorial...

Your code says something like this:

$dbh = foo();

Here, the $dbh variable is a global. When using strict, you need to fully
specify the package name for globals, like this:

$Package::dbh = foo();

Or, you can declare it as a lexical variable like this:

my $dbh = foo();

See 'perldoc strict' for more info, in the "strict vars" section.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
Andy Stevenson

2004-07-14, 8:56 pm

Sherm Pendley wrote:
> Andy Stevenson wrote:

[snip]
> Looks like you're using 'strict' - which is a *very* good idea - but
> you're trying to copy code from a badly-written tutorial that didn't
> bother using it. You should try to find a better tutorial...
>
> Your code says something like this:
>
> $dbh = foo();
>
> Here, the $dbh variable is a global. When using strict, you need to
> fully specify the package name for globals, like this:
>
> $Package::dbh = foo();
>
> Or, you can declare it as a lexical variable like this:
>
> my $dbh = foo();
>
> See 'perldoc strict' for more info, in the "strict vars" section.
>
> sherm--


Got it, thanks for that. So it's not a modules problem after all :) However,
now that I'm here...

I've altered the code thus:

#!perl

#Windows-based Perl/DBI/MS Access example
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use DBI;

my ($dbh, $sqlstatement, $sth);
my @row = ();

#open connection to Access database
$dbh = DBI->connect('DBI:ODBC:Clients');

#prepare and execute SQL statement
$sqlstatement="SELECT ClientName,ClientEmail FROM billing";
$sth = $dbh->prepare($sqlstatement);
$sth->execute ||
die "Could not execute SQL statement ... maybe invalid?";

#output database results
while (@row=$sth->fetchrow_array)
{ print "@row\n" }

I'm now getting the dreaded 'Infernal' Server error :(

Any ideas?

Thanks


Sherm Pendley

2004-07-14, 8:56 pm

Andy Stevenson wrote:

> #output database results
> while (@row=$sth->fetchrow_array)
> { print "@row\n" }


Shouldn't that be:

{ print "@row\n"; }

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
Eric Bohlman

2004-07-15, 3:57 am

"Andy Stevenson" <no@chance.com> wrote in
news:vjjJc.986$fO5.781@newsfe6-win.ntli.net:

> I've altered the code thus:
>
> #!perl
>
> #Windows-based Perl/DBI/MS Access example
> use CGI qw(:standard);
> use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
> use strict;
> use DBI;


You want a 'use warnings;' in there.

>
> my ($dbh, $sqlstatement, $sth);
> my @row = ();


For maintainability, it's best to declare variables right where they're
first used, and in as narrow a scope as possible.

>
> #open connection to Access database
> $dbh = DBI->connect('DBI:ODBC:Clients');


So most programmers would prefer to start that off with 'my $dbh' rather
than declaring $dbh previously. However, there's a more important problem
here; you haven't checked to see if your attempt to connect was successful.
If it wasn't, you'll bomb out when you try to use $dbh as a handle.


> #prepare and execute SQL statement
> $sqlstatement="SELECT ClientName,ClientEmail FROM billing";


my $sqlstatement...

> $sth = $dbh->prepare($sqlstatement);


my $sth... and you should also check that this was successful before using
$sth.

> $sth->execute ||
> die "Could not execute SQL statement ... maybe invalid?";


Get in the habit of writing 'or die' rather than '|| die'; while it's not a
problem here, it's easy to get burned by precedence with the latter form.

>
> #output database results
> while (@row=$sth->fetchrow_array)


while (my @row...

> { print "@row\n" }


As Sherm pointed out, you're missing a semicolon here.

> I'm now getting the dreaded 'Infernal' Server error :(


You also haven't output any content-type header before you started
printing.
John W. Krahn

2004-07-15, 8:55 am

Sherm Pendley wrote:
>
> Andy Stevenson wrote:
>
>
> Shouldn't that be:
>
> { print "@row\n"; }


perldoc perlsyn
[snip]
Simple statements

The only kind of simple statement is an expression evalu_
ated for its side effects. Every simple statement must be
terminated with a semicolon, unless it is the final state_
ment in a block, in which case the semicolon is optional.



John
--
use Perl;
program
fulfillment
Sherm Pendley

2004-07-15, 3:56 pm

John W. Krahn wrote:

> Sherm Pendley wrote:
>
> [snip]


That's why I wrote it in the form of a question - I knew that a semicolon
would be OK there, but wasn't certain if it was required.

Thanks for the info.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
Andy Stevenson

2004-07-15, 3:56 pm

Xref: kermit comp.lang.perl.modules:45477

Eric Bohlman wrote:
[snip]

Brilliant! Thank you all. Script now working.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com