2

Can someone give me some direction on the best way to do when sharing a $dbh variable between "objects" in different .pm files.

For instance, my main module say Foo.pm has a new constructor, etc and I could give it a dbh or create a dbh and then share it by passing it as a parameter to the new constructor for Bar.pm, and then re-assigning inside Bar->new, but that seems like I'm doing a lot of work managing this variable.

Is this a simple, yet elegant way to do this? I've researched Exporter and a few other examples, but none seem to be straight forward.

Thanks!

4
  • 2
    Redesign your modules. You should have only one that writes to the database. Commented Apr 26, 2013 at 23:39
  • 2
    Consider DBIx::Connector. Your main application can instantiate a connector object, and may provide it to the classes the app uses. Connector objects can share a connection, or spin up a new one if the existing one dies. This is one of the ways I deal with database interaction in full Mojolicious apps. Commented Apr 27, 2013 at 3:23
  • 1
    Try the Singleton pattern. You can write your own or try a Singleton base class from CPAN. Commented Apr 27, 2013 at 7:22
  • 2
    @Borodin, you're right, and that module is called DBI. If that's not the one you was referring to, then your statement is too strong to be true. Having a layer between DB and your business logic is a good practice in some cases (and then you better have one such layer indeed), but not in all cases and not even in the majority of cases, I'd say. There's more than one way to do it, you know. Commented Apr 27, 2013 at 11:08

2 Answers 2

2

I suppose that what you actually want is to take the control over $dbh creation out of the code that works with it. Most trivial way is, well,

my $dbh;
sub get_dbh {
    if ( $dbh is bad ) {
       reconnect or whatever
    }

    return $dbh || die;
}

And then in your code access it like

get_dbh()->do("your sql");

You could put that get_dbh() function to a separate module and call it from anywhere in your project - as usual with perl, it will be included only once and its local static variable $dbh will exist in only one copy within the perl process.

There are many possible ways to achieve that, writing a function like described above (and maybe passing a reference to that function instead of passing the $dbh) is one. There are plenty of others, depending on your design and personal taste - a singleton class, a variable tied to the function described above, or even a class that imitates DBI... That's up to you, but that should be one piece of code, spreading this logic all over your project is a bad idea.

Sign up to request clarification or add additional context in comments.

Comments

-1

If you're using Moose to build your object, you could encapsulate your database handle in a role and require it into classes that need the database access.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.