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

Hi, I'm still rewritting my script and still got little error on my log but doesn't corrupt my cgi execution. Here is the code for helloWorld.cgi
#!/usr/bin/perl -w use strict; use warnings; use CGI; use Search2; my $query = CGI->new ; my $search = Search2->new; loadPage (); sub loadPage { my $page = ""; $page = $query->param("page"); if ($page eq 'search') { $search->say_hello(); }else { print "Content-Type: text/html\n\n"; print "error"; } }
package Search2
#!/usr/bin/perl -w package Search2; use strict; use MyDB; use SharedVariable qw ($action $session_dir $dir $dirLang $dirError $i +mgdir $session_id $can_do_gzip $current_ip $lang $LANG %ARTICLE %SESS +ION %SERVER %USER $CGISESSID %LABEL %ERROR %VALUE $COMMANDID %COMMAND + %DATE %PAYPALL $INDEX %LINK $query $session $host $t0 $client); use LoadProperties; our $db = MyDB->new(); our $lp = LoadProperties->new(); sub new { my $class = shift; my ($opts)= @_; my $self = {}; return bless $self, $class; } sub search { } sub do_search { } sub do_search_indexed { } sub say_hello () { print "Content-Type: text/html\n\n"; print "Hello Search Package"; } BEGIN { use Exporter (); @DB::ISA = qw(Exporter); @DB::EXPORT = qw(); @DB::EXPORT_OK = qw(new say_hello); } 1;
package SharedVariable
package SharedVariable; use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); use Time::HiRes qw(gettimeofday); use Compress::Zlib; use CGI::Session qw/-ip-match/; use vars qw (%ENV $session_dir $cookie $page $dir $dirLang $dirError $ +imgdir $action $t0 $session_id $can_do_gzip $current_ip $lang $LANG % +ARTICLE %SESSION %SERVER %USER $CGISESSID %LABEL %ERROR %VALUE $COMMA +NDID %COMMAND %DATE %PAYPALL $INDEX %LINK $query $session $host $t0 + $client); #Set the values to the global variables $query = CGI->new ; $cookie = ""; $current_ip = $ENV{'REMOTE_ADDR'}; $client = $ENV{'HTTP_USER_AGENT'}; $t0 = gettimeofday(); $host = "http://avant-garde.no-ip.biz"; %ERROR = ();%LABEL = ();$LANG = "";%LINK = ();%ARTICLE = ();%SESSION = + ();%SERVER = (); % $action = $query->param('action'); $page = $query->param("page"); $session_id = $query->param('session'); $can_do_gzip = ($ENV{'HTTP_ACCEPT_ENCODING'} =~ /gzip/i) ? 1 : 0; $dir = "/home/alexandre/apache/site/recordz1/"; $dirLang = "/home/alexandre/apache/site/recordz1/lang"; $dirError = "/home/alexandre/apache/site/recordz1/lang"; $imgdir= "/home/alexandre/apache/site/recordz1/upload"; $session_dir = "/home/alexandre/apache/site/recordz/sessions"; $action = $query->param('action'); $session_id = $query->param('session'); #load html label loadLanguage (); #load error label loadError(); sub new { my $class = shift; my ($opts)= @_; my $self = {}; return bless $self, $class; } sub loadLanguage { $lang = "FR"; $lang = $query->param("lang"); #$lang = substr ($ENV{'HTTP_ACCEPT_LANGUAGE'},0,2); #$lang =~ s/[^A-Za-z0-9 ]//; $lang = uc ($lang); open (FILE, "<$dirLang/$lang.conf") or die "cannot open file $dirL +ang/$lang.conf"; while (<FILE>) { (my $label, my $value) = split(/=/); $SERVER{$label} = $value; } close (FILE); } sub loadError { $lang = lc ($query->param('lang'));#=~ s/[^A-Za-z0-9 ]//; #$lang=~ s/[^A-Za-z0-9 ]//; open (FILE, "<$dirError/$lang.error.conf") or die "cannot open fil +e $dirError/$lang.error.conf"; while (<FILE>) { (my $label, my $value) = split(/=/); $ERROR{$label} = $value; } close (FILE); } BEGIN { use Exporter (); @SharedVariable::ISA = qw(Exporter); @SharedVariable::EXPORT = qw(gettimeofday()); @SharedVariable::EXPORT_OK = qw ($session_dir $cookie $page $ac +tion $dir $dirLang $dirError $imgdir $session_id $can_do_gzip $curren +t_ip $lang $LANG %ARTICLE %SESSION %SERVER %USER $CGISESSID %LABEL %E +RROR %VALUE $COMMANDID %COMMAND %DATE %PAYPALL $INDEX %LINK $query $ +session $host $t0 $client); } 1;
package MyDB
#!/usr/bin/perl -w package MyDB; use strict; use DBI; use SharedVariable qw ($action $dir $dirLang $dirError $imgdir $sessio +n_dir $session_id $can_do_gzip $current_ip $lang $LANG %ARTICLE %SES +SION %SERVER %USER $CGISESSID %LABEL %ERROR %VALUE $COMMANDID %COMMAN +D %DATE %PAYPALL $INDEX %LINK $query $session $host $t0 $client); our $dbh = DBI->connect( "DBI:mysql:recordz:localhost", "root", "XXXXX +XXXXXXXX",{ RaiseError => 1, AutoCommit => 1 } ); sub new { my $class = shift; my ($opts)= @_; my $self = {}; return bless $self, $class; } sub sqlConnect { my $dbname = shift || ''; my $dbusername = shift || ''; my $dbpassword = shift || ''; $dbh = DBI->connect( "DBI:mysql:recordz:localhost", "root", "a +lexandre",{ RaiseError => 1, AutoCommit => 1 } ); if (!$dbh) { } kill 9, $$ unless $dbh; } sub sqlSelect { my $from = shift || ''; my $select = shift || ''; my $where = shift || ''; my $other = shift || ''; my $other2 = shift || ''; my $sql="SELECT $select "; $sql.="FROM $where "; $sql.="WHERE $other "; #$sql.="$other"; #$sql.="$other2"; #$sql = $dbh->quote ($sql); my ($c)=$dbh->prepare($sql) or die "Sql has gone to hell\n"; + + + + + + #print "Content-Type: text/html\n\n"; + + + + + + #print "SQL : $sql \n"; # print "Content-Type: text/html\n\n"; # print "SQL : $sql \n"; + + + + + + + + + + + + if(not ($c->execute())) { + + + + + + my $err=$dbh->errstr; + + + + + + return undef; } my (@r)=$c->fetchrow(); $c->finish(); return @r; } sub sqlSelect1 { my $from = shift || ''; my $select = shift || ''; my $where = shift || ''; my $other = shift || ''; my $other2 = shift || ''; my $sql="SELECT $select "; $sql.="FROM $where "; $sql.="WHERE $other "; #$sql.="$other"; #$sql.="$other2"; #$sql = $dbh->quote ($sql); # print "Content-Type: text/html\n\n"; # print "SQL : $sql \n"; my ($c)=$dbh->prepare($sql) or die "Sql has gone to hell\n"; + + + + + + #print "Content-Type: text/html\n\n"; + + + + + + #print "SQL : $sql \n"; + + + + + + + + + + + + if(not ($c->execute())) { + + + + + + my $err=$dbh->errstr; + + + + + + return undef; } my (@r)=$c->fetchrow(); $c->finish(); return @r; } sub sqlSelect2 { my $from = shift || ''; my $select = shift || ''; my $where = shift || ''; my $other = shift || ''; my $other2 = shift || ''; my $sql="SELECT $select "; $sql.="FROM $where "; $sql.="WHERE $other "; #$sql.="$other"; #$sql.="$other2"; #$sql = $dbh->quote ($sql); my ($c)=$dbh->prepare($sql) or die "Sql has gone to hell\n"; if(not ($c->execute())) { my $err=$dbh->errstr; return undef; } my (@r)=$c->fetchrow(); $c->finish(); return @r; } sub sqlInsert { my $invoke = shift || ''; my($table,%data)=@_; my($names,$values); $dbh||=sqlConnect(); foreach (keys %data) { if (/^-/) {$values.="\n ".$data{$_}.","; s/^-//;} else { $values.="\n ".$dbh->quote($data{$_}).","; } $names.="$_,"; } chop($names); chop($values); my $sql="INSERT INTO $table ($names) VALUES($values)\n"; #$sql = $dbh->quote ($sql); if(!$dbh->do($sql)) { my $err=$dbh->errstr; } } sub sqlInsert2 { my $invoke = shift || ''; my($table, %data)=@_; print "Content-type: text/html\n\n"; my($names,$values); $dbh||=sqlConnect(); foreach (keys %data) { if (/^-/) { $values.="\n ".$data{$_}.","; print "if\n"; print $values; s/^-//; $names.="$_,"; #INSERT INTO ( NULL, 'text', 'question', 'ref_eme +tteur', 'date') VALUES (2013-05-31 15:25:28,,1,commentaire,this is th +e description #INSERT INTO ( 'text', 'question', NULL, 'ref_eme +tteur', 'date') VALUES (,1,2013-05-31 15:26:49,commentaire,this is th +e description } else { $values.="\n ".$data{$_}.","; s/^-//; $names.="$_,"; } } chop($names); chop($values); my $sql="INSERT INTO $table ($names) VALUES ($values)\n"; #$sql = $dbh->quote ($sql); #print "Content-Type: text/html\n\n"; #print "$sql <br />"; if(!$dbh->do($sql)) { my $err=$dbh->errstr; } } sub sqlUpdate { my $invoke = shift || ''; my ($table, $where, %data)=@_; my $sql="UPDATE $table SET"; foreach (keys %data) { if (/^-/) { s/^-//; $sql.=" $_ = $data{-$_} " . ","; } else { $sql.=" $_ = ".$dbh->quote($data{$_}).","; } } chop($sql); $sql.=" WHERE $where "; #print "Content-Type: text/html\n\n"; #print "SQL : $sql \n"; if(!$dbh->do($sql)) { my $err=$dbh->errstr; } } sub sqlUpdate3 { my $invoke = shift || ''; my $table = shift || ''; my $where = shift || ''; my %data = shift || ''; my $other = shift || ''; my ($names,$values); my $sql="UPDATE $where SET"; foreach (keys %data) { if (/^-/) {$values.="\n ".$data{$_}.","; s/^-//;} else { $values.="\n ".$dbh->quote($data{$_}).","; } $names.="$_,"; chop($names); chop($values); $sql.=" $names WHERE $other "; } print "Content-Type: text/html\n\n"; print "SQL : $sql \n"; $dbh->do($sql); $dbh->disconnect(); } sub sqlUpdate1 { my $invoke = shift || ''; my $table = shift || ''; my $where = shift || ''; my %data = shift || ''; my $other = shift || ''; my ($names,$values); my $sql="UPDATE $where SET"; foreach (keys %data) { if (/^-/) {$values.="\n ".$data{$_}.","; s/^-//;} else { $values.="\n ".$dbh->quote($data{$_}).","; } $names.="$_,"; chop($names); chop($values); $sql.=" $names WHERE $where "; } $dbh->do($sql); $dbh->disconnect(); } sub sqlSelectMany { my $from = shift || ''; my $select = shift || ''; my $where = shift || ''; my $other = shift || ''; my $other2 = shift || ''; my $sql="SELECT $select "; $sql.="FROM $where "; $sql.="WHERE $other "; my $c=$dbh->prepare($sql); #print "Content-Type: text/html\n\n"; # print "SQL : $sql \n"; if($c->execute()) { return $c; } else { $c->finish(); my $err=$dbh->errstr; return undef; kill 9,$$ } } sub sqlDelete { my $fromtable = shift || ''; my $condition = shift || ''; my $sql = ''; if ($condition) { $sql = "DELETE from $fromtable WHERE $condition"; } else { $sql = "DELETE from $fromtable"; } #print "Content-Type: text/html\n\n"; #print "$sql"; #$sql = $dbh->quote ($sql); if (!$dbh->do($sql)) { my $err=$dbh->errstr; } } BEGIN { use Exporter (); @DB::ISA = qw(Exporter); @DB::EXPORT = qw(); @DB::EXPORT_OK = qw(new sqlSelectMany1 sqlConnect sqlSelect sqlI +nsert sqlUpdate sqlSelectMany sqlDelete); } 1;
An error appear when I run my script https://avant-garde.no-ip.biz/cgi-bin/helloWorld.cgi?page=search&lang=FR
[Fri Dec 28 14:11:18 2018] [error] [client 127.0.0.1] [Fri Dec 28 14:1 +1:18 2018] Search2.pm: Subroutine new redefined at Search2.pm line 13 +.

2018-12-30 Athanasius fixed opening code tag, linkified link (broken), and added code tags around final error message

Replies are listed 'Best First'.
Re: Subroutine new redefined
by Corion (Patriarch) on Dec 28, 2018 at 13:41 UTC

    You are posting far too much code. Please reduce it to something that still reproduces the problem but is much shorter.

    My guess is that one of your modules exports a subroutine new. As there is no way that I see that one of the modules you show is exporting a subroutine named new, it must be the LoadProperties module that exports a subroutine new. This triggers (rightfully) the warning about redefining that subroutine later on in Search2.

    Also, what coding style is this and what problem is it supposed to solve:

    BEGIN { ... @DB::EXPORT_OK = qw(new say_hello); ... }

    The DB package is magic and reserved for the Perl debugger. Exporting symbols is usually done through global variables in the package doing the exporting. Methods of an object should not be exportable at all in any case. In my opinion, only your SharedVariables module should export its shared variable names, and none of the other modules should export anything.

      Thx, I will make the change you are cited !
Re: Subroutine new redefined
by 1nickt (Canon) on Dec 28, 2018 at 16:01 UTC

    Hi, you might like SQL::Abstract::More for building your SQL statements

    Hope this helps!


    The way forward always starts with a minimal test.