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

my $DB = ref_to_db(); #at this point it's all good. i can call prepare like # $$DB->prepare(qq| etc etc etc.... #this would be fine too closenow(\$$DB); #now... # $DB is a reference to an open DB connection, # how do it reference to it in a hash, pass a reference # to the hash to a sub, then dereference to the DB ???!!! my %Booga - (DB => \$$DB ); closeplease(\%Booga); sub ref_to_db { my $DB = DBI->connect("DBI:mysql:dat...... etc etc return \$DB; } sub closenow { $_[0]->disconnect; } sub closeplease { my $Booga = $_[0]; #im at a loss here, is this way off? $$Booga{DB}->disconnect; }

I don't know where this is breaking my cable, something about it is off- any comments.. The thing is giving me a nervous twitch in my left eye serioulsy.

  • Comment on pass reference, to a hash with a reference to an open db handle, to a sub - help
  • Download Code

Replies are listed 'Best First'.
Re: pass reference, to a hash with a reference to an open db handle, to a sub - help
by Tanktalus (Canon) on Mar 06, 2006 at 22:08 UTC

    References, like pointers, can be hard. But, in perl, I find that I can mostly ignore that. I still remember learning pointers in C - took me 2 months to learn everything else in that book, but 2 months just to get my head around this one thing. Since then, everything was downhill.

    Anyway, back to your problem. Is there a reason why you're returning a reference to the DBD driver you get from DBI->connect? You could just as easily do:

    sub ref_to_db { my $db = DBI->connect(...); return $db; }
    That's because $db already is a reference to a DBD object.

    But, for a minute, let's say this is really a simplification of your code, and there really is a reason to return a reference to it. In that case, you need to realise that since $DB (in your main code) is a reference to a DBD reference, then $$DB is merely a reference, and \$$DB is exactly the same as $DB. The same applies to your initialisation of %Booga - you can just do my %Booga = (DB => $DB). Ok, so that simplifies your call to closenow, but doesn't simplify closenow yet.

    For the closenow function, you are missing one layer of indirection. It's probably easier if we use an intermediate variable (I did this in C++ all the time where I'd create extra reference variables just to save on effort trying to get my mind around too many layers at once):

    sub closenow { my $rDB = shift; my $DB = $$rDB; $DB->disconnect(); }
    That's probably done in a single line like ${$_[0]}->disconnect(), but I find that's just a bit too much line noise for my liking.

    Finally, we should be able to extrapolate all this into your closeplease function:

    sub closeplease { my $Booga = shift; my $DB = ${$Booga->{DB}}; $DB->disconnect(); }
    The real purpose of passing in references like this is to be able to change the caller, such as:
    sub closeitnow { my $rDB = shift; $rDB = $rDB->{DB} if ref $DB eq 'HASH'; my $DB = $$rDB; $DB->disconnect(); $rDB = undef; # now the caller's reference is gone, too! }
    But I try to discourage this. It's too much action-at-a-distance for me. Instead, get rid of the extra layer of indirection, and your brain won't explode :-)

Re: pass reference, to a hash with a reference to an open db handle, to a sub - help
by Fletch (Bishop) on Mar 06, 2006 at 22:06 UTC

    By taking a reference to $DB in ref_to_db you're introducing an unneeded extra layer of indirection. A DBI handle is already a reference; just return $DB and everything will work. For completeness sake, ${ $Booga }->disconnect (or more succinctly ${ $_[0] }->disconnect) would dereference the extra layer of indirection then call the disconnect method on the resulting handle.