tudor has asked for the wisdom of the Perl Monks concerning the following question:

dear monks i thank you for helping me so fast in my previous problem and i come now with another one hopping that i would find some answers. this time i thank you in advance! i keep getting this error when i use strict:
Global symbol "$dbh" requires explicit package name at C:\Xitami\cgi-bin\ifr_par2.pl line 15.
Global symbol "$sth" requires explicit package name at C:\Xitami\cgi-bin\ifr_par2.pl line 22.
Global symbol "$dbh" requires explicit package name at C:\Xitami\cgi-bin\ifr_par2.pl line 22.
Global symbol "$dbh" requires explicit package name at C:\Xitami\cgi-bin\ifr_par2.pl line 25.
Global symbol "$sth" requires explicit package name at C:\Xitami\cgi-bin\ifr_par2.pl line 29.
Global symbol "$sth" requires explicit package name at C:\Xitami\cgi-bin\ifr_par2.pl line 57. ,otherwise the script works
use CGI; use CGI::Carp qw(fatalsToBrowser); use DBI; use strict; my $db_name="scent"; my $o = new CGI; my $tip2 =$o -> param("tip2"); my $DSN="dbi:mysql:$db_name"; my $user="root"; my $pass=""; $dbh = DBI -> connect($DSN,$user,$pass); #return; my $SQL= "select * from par;"; eval { $sth = $dbh -> prepare($SQL); }; if ($@) { $dbh -> disconnect; print $@ } else { $sth -> execute; } ........................................

Replies are listed 'Best First'.
Re: Global symbol "$dbh" requires explicit package name
by castaway (Parson) on Oct 23, 2004 at 12:26 UTC
    You're 'using' strict.pm, yet you are attempting to create global variables by writing "$dbh = ... ". Just change that line to my $dbh =  DBI -> connect($DSN,$user,$pass);, and the same for $sth etc. That makes them local to the file and thus scoped.

    C.

      To clear things up concerning $sth, you'll have to say my $sth; before the eval instead of just adding my before the assignment like castaway suggested you do for $dbh, or else the variable won't exist when it comes to calling $sth->excute.

      use CGI; use CGI::Carp qw(fatalsToBrowser); use DBI; use strict; my $db_name="scent"; my $o = new CGI; my $tip2 =$o -> param("tip2"); my $DSN="dbi:mysql:$db_name"; my $user="root"; my $pass=""; my $dbh = DBI -> connect($DSN,$user,$pass); <------ #return; my $SQL= "select * from par;"; my $sth; <------ eval { $sth = $dbh -> prepare($SQL); }; if ($@) { $dbh -> disconnect; print $@ } else { $sth -> execute; }

      or even:

      use CGI; use CGI::Carp qw(fatalsToBrowser); use DBI; use strict; my $db_name="scent"; my $o = new CGI; my $tip2 =$o -> param("tip2"); my $DSN="dbi:mysql:$db_name"; my $user="root"; my $pass=""; my $dbh = DBI -> connect($DSN,$user,$pass); <------ #return; my $SQL= "select * from par;"; my $sth = eval { $dbh -> prepare($SQL) }; <------ if ($@) { $dbh -> disconnect; print $@ } else { $sth -> execute; }
Re: Global symbol "$dbh" requires explicit package name
by techy (Scribe) on Oct 23, 2004 at 12:34 UTC
    Tudor,

    As mentioned in other replies on here, from the program listing, it appears that $dbh and $sth should be declared with my, similar to what is being used for other variables in your program. $sth, because it is first used in an eval block, should be first declared outside the block with my $sth;.

    The eval for $sth = $dbh->prepare($SQL); may be overkill for error checking, normally for preparing statements I use code like my $sth = $dbh->prepare($SQL) or die "..."; depending on program requirements. Changing that can also allow you to declare the variable with "my" and assign it in one statement. Also, I noticed that the prepare statement is being checked for errors, but not execute.

    Thanks,

    techy
    When art critics get together they talk about Form and Structure and Meaning. When artists get together they talk about where you can buy cheap turpentine. -- Pablo Picasso

Re: Global symbol "$dbh" requires explicit package name
by tachyon (Chancellor) on Oct 23, 2004 at 12:32 UTC