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

Hello monks, i have a weird problem, i have a database module called DatosUach in this module i create a dbi handler ($self->{dbh}) and use it for common database operations. a friend of mine is checking out CGI::Session, this module needs a dbi handler, so we (i know it's an OO aberration, but this was just a test) took it from an instance of a DatosUach object and use it on the constructor of CGI::Session. But for our surprise we got a DBI error telling us that the dbi handler we used on CGI::Session was not a valid refrence, we tried connecting using DBI->connect directly and it worked fine. I checked the references from both handlers are the same. Now, my question is: does this have something to do with scope?? i don't know it's very weird, excuse me if this is kind of confusing but that's how i'm feeling right now here's the code:
sub new { my $proto=shift; my $class=ref($proto) || $proto; my $myself; my $db; my $session; my $myCGI; my $sid; my $cookie ; my $dbh; #my object that contains a dbi handler $db = DatosUach::new($datos{HOST},$datos{DRIVER},$datos{D +ATABASE},$datos{USUARIO},$datos{PASSWORD}); #direct instance of DBI $dbh = DBI->connect(dbi:$datos{DRIVER}:dbname=$datos{DATABASE} +;host=$datos{HOST}, $datos{USUARIO},$datos{PASSWORD}); if ( !defined $dbh ) { die "Cannot connect to database!\n"; } print header. "checando:"; print "<h1>REF=---".ref($db->{dbh})."---</h1>"; print "checando el otro". ref($dbh); my $DBIHandler = $db->{dbh}; $DBIHandler->do("insert into sessions values ('1','1')"); print "Checando".ref ($DBIHandler); $CGI::POST_MAX =1024 * 25; $myCGI = new CGI; $sid = $myCGI->cookie("CGISESSID") || undef; #print header.$sid; if (defined $sid) { $session = new CGI::Session("driver:PostgreSQL", $sid, +{Handle=>$db->{dbh}}); print header."Existe Sid" ; }else{ $session = new CGI::Session("driver:PostgreSQL", undef, {H +andle=>$db->{dbh}}); $cookie = $myCGI->cookie(CGISESSID => $session->id); $sid = $session->id; print $myCGI->header( -cookie=>$cookie ); print "No existe sid"; }
thanks for your help


ignorance, the plague is everywhere
--guttermouth

Edit by castaway, extra empty lines removed

Replies are listed 'Best First'.
Re: weird reference problem
by japhy (Canon) on Sep 02, 2004 at 21:19 UTC
    $db = DatosUach::new($datos{HOST}, $datos{DRIVER},$datos{DATABASE},$datos{USUARIO}, $datos{PASSWORD});
    Are you sure that shouldn't be:
    $db = DatosUach->new( $datos{HOST}, $datos{DRIVER}, $datos{DATABASE}, $datos{USUARIO}, $datos{PASSWORD} );
    _____________________________________________________
    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      nope, the DatosUach object works well, the reference to a dbi object that i extract from it ($db->{dbi}) works well too. the weird part is when i give it to the CGI::Session constructor.


      ignorance, the plague is everywhere
      --guttermouth
Re: weird reference problem
by macPerl (Beadle) on Jan 19, 2005 at 15:52 UTC
    imcsk8,

    did you find a resolution to this issue. I have come on s'thing which seems v. similar (am testing CGI::Session also):

    2 possibilities:

    1. create direct connection

    my $dbh = ablib::connectMe($pIn{'admUn'}, $pIn{'admPw'});

    2. using session->param

    my $dbh = $session->param("dbh");

    Then I pass in identical way to query sub

    my @loopData = ablib::menuSQL($dbh, $myID);

    #1 works perfectly, #2 fails on $dbh->prepare(...)

    Interrogation of vars and both look ok :

    #1 DBI::db=HASH(0x83caa6c)

    #2 DBI::db=HASH(0x84b7514)

    any ideas ?