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 :-)


In reply to Re: pass reference, to a hash with a reference to an open db handle, to a sub - help by Tanktalus
in thread pass reference, to a hash with a reference to an open db handle, to a sub - help by leocharre

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.