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

Hi, I rewrite some perl code and I found a problem creating a new object here is my package

#!/usr/bin/perl -w package DB; 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", "passw +ord",{ RaiseError => 1, AutoCommit => 1 } ); sub new { my $class = shift; my ($opts)= @_; my $self = {}; return bless $self, $class; } sub sqlConnect { local our $dbname = shift || ''; local our $dbusername = shift || ''; local our $dbpassword = shift || ''; $dbh = DBI->connect($dbname, $dbusername, $dbpassword); if (!$dbh) { } kill 9, $$ unless $dbh; } sub sqlSelect { local our $select = shift || ''; local our $from = shift || ''; local our $where = shift || ''; local our $other = shift || ''; local our $sql="SELECT $select "; $sql.="FROM $from " if $from; $sql.="WHERE $where " if $where; $sql.="$other" if $other; #$sql = $dbh->quote ($sql); #print "Content-Type: text/html\n\n"; #print "sql : $sql\n"; local our ($c)=$dbh->prepare($sql) or die "Sql has gone to hell\n +"; if(not ($c->execute())) { local our $err=$dbh->errstr; return undef; } local our (@r)=$c->fetchrow(); $c->finish(); return @r; } sub sqlInsert { local our ($table,%data)=@_; local our ($names,$values); $dbh||=sqlConnect(); foreach (keys %data) { if (/^-/) {$values.="\n ".$data{$_}.","; s/^-//;} else { $values.="\n ".$dbh->quote($data{$_}).","; } $names.="$_,"; } chop($names); chop($values); local our $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)) { local our $err=$dbh->errstr; } } sub sqlUpdate { local our ($table, $where, %data)=@_; local our $sql="UPDATE $table SET"; foreach (keys %data) { if (/^-/) { s/^-//; $sql.=" $_ = $data{-$_} " . ","; } else { $sql.=" $_ = ".$dbh->quote($data{$_}).","; } } chop($sql); $sql.=" WHERE $where "; # $sql = $dbh->quote ($sql); # print "Content-Type: text/html\n\n"; # print "$sql"; if(!$dbh->do($sql)) { local our $err=$dbh->errstr; } } sub sqlSelectMany { local our $select = shift || ''; local our $from = shift || ''; local our $where = shift || ''; local our $other = shift || ''; local our $sql="SELECT $select "; $sql.="FROM $from " if $from; $sql.="WHERE $where " if $where; $sql.="$other" if $other; #print "Content-Type: text/html\n\n"; #print "$sql"; #$sql = $dbh->quote ($sql); local our $c=$dbh->prepare($sql); if($c->execute()) { return $c; } else { $c->finish(); local our $err=$dbh->errstr; return undef; kill 9,$$ } } sub sqlDelete { local our $fromtable = shift || ''; local our $condition = shift || ''; local our $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)) { local our $err=$dbh->errstr; } } BEGIN { use Exporter (); @DB::ISA = qw(Exporter); @DB::EXPORT = qw(); @DB::EXPORT_OK = qw(new sqlConnect sqlSelect sqlInsert sqlUpdate + sqlSelectMany sqlDelete); } 1;

And into another module I'm trying to create a DB object like this way
my $db = DB->new(); but I'm getting the following exception
Can't locate object method "new" via package "DB" at LoadProperties.pm line 8.

Replies are listed 'Best First'.
Re: object creation
by davido (Cardinal) on May 22, 2013 at 13:58 UTC

    First, you don't need to export your object methods, or even inherit from Exporter in object oriented code. Next, we would need to see how you're using the module in LoadProperties.pm.

    Also, instead of setting up a package global holding a database handle, use DBIx::Connector, which will re-initialize database handles when they become invalid (which does happen). In fact, some of your module's functionality is already done well by DBIx::Connector.

    I'm not sure why you're using so many localized package globals anyway; what's wrong with lexically scoped variables via 'my' within your subroutines? You must have a good reason that I'm not seeing.


    Dave

Re: object creation
by hdb (Monsignor) on May 22, 2013 at 14:41 UTC

    There might be a module "DB" already installed on your Perl. Check with the following code which one you "use":

    use DB; print $INC{"DB.pm"};
      > There might be a module "DB" already installed on your Perl.

      sure it belongs to the perldebugger and is available for every Perl

      so better don't try to run this code with -d =)

      Cheers Rolf

      ( addicted to the Perl Programming Language)

      update
      lanx@nc10-ubuntu:~$ corelist DB DB was first released with perl 5.006

        Runs fine for me:

        C:\strawberry-perl-5.16.3.1-64bit-portable\scripts>perl -d -e "use DB; + print $INC{'DB.pm'};" Loading DB routines from perl5db.pl version 1.37 Editor support available. Enter h or 'h h' for help, or 'perldoc perldebug' for more help. C:/strawberry-perl-5.16.3.1-64bit-portable/perl/lib/DB.pm