We do it like this:
package DBH; use strict; use DBI (); my $mysql_server = "You fill this in"; my $mysql_user = "You fill this in"; my $mysql_password = "You fill this in"; my $mysql_db = "You fill this in"; sub connect { if (defined $DBH::conn) { my $ret; eval { $ret = DBH->ping; }; if (!$@ && $ret) { return $DBH::conn; } } $DBH::conn = DBI->connect( "DBI:mysql:$mysql_db;$mysql_server",$mysql_user,$mysql_passwor +d, { PrintError => 1, RaiseError => 0, } ) || die $DBI::errstr; #Assume application handles this return $DBH::conn; } sub ping { my $ret = 0; if (time - $DBI::lastPing < 10) { return 1; } eval { local $SIG{__DIE__} = sub { return (0); }; local $SIG{__WARN__} = sub { return (0); }; # adapt the select statement to your database: $ret = $DBH::conn->do('select 1'); $DBI::lastPing = time; #record time in seconds }; $debug && print STDERR "DBH.pm: pinging DB handle: $ret\n"; return ($@) ? 0 : $ret; } 1; # now you can use this in your script: #!/usr/bin/perl -w use strict; use DBH; my $dbh = DBH->connect(); my $sth = $dbh->prepare("your query...");


"We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.

In reply to Re: object method question by jbrugger
in thread object method question by Zarathustra

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.