perl references to objects

They have: 447 posts

Joined: Oct 1999

hi, here's my problem:

i have a perl module named PGDB which facilitates connecting and querying a postgresql database. i also have a module named Sessions which handles user log in, validation, and management of session variables. Sessions requires a PGDB object to access the database.

So if i have a script that needs both a Session object and a PGDB object i do something like this...

----------------
use PGDB;
use Session;

my $pg = new PGDB;
my $ses = new Session;
----------------------

this works fine, but results in two database connections. both the PGDB object and the Session object create their own connections.

What i'd like to do is use the same database connection if a script requires both objects.. kind of like this:

#------
use PGDB;
use Session;

my $pg = new PGDB; # create a database connection object
my $ses = new Session($pg); #pass the reference to the db object to
# the session object so it can use the
# existing connection

#---------------
#Session new sub looks like this:
sub new()
{ my ($self,$pgdbc) = @_;
$DBC = new PGDB($pgdbc);

...
}
#PGDB new sub:
sub new()
{ my ($self,$pgdbc) = @_;
if($pgdbc)
{ $PGDBC = $pgdbc;
} else
{ $PGDBC = Pg::connectdb($connstring);
}

...
}
----------------

this doesnt work... is it possible to share object references between objects? any ideas on how to achieve this?

They have: 601 posts

Joined: Nov 2001

Why not create another package with the database routines in them, and expost all vars out of that package into both your scripts?

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.