I am trying to open a database connection in a subroutine, and pass the handle ($dbh) as the result of a successful open.
my $dbh; sub connectdb {# connect to database using 3 parameters; return databa +se handle my ($dbstring, $dbdepot, $password) = @_; print "attempt to connect to database $dbstring :$dbdepot\n"; system "time /T"; my $dbhi = DBI->connect($dbstring,$dbdepot,$password, { AutoCommit => 0, RaiseError => 1, PrintError => 1 }) or warn "can't connect to database $dbstring :$dbdepot :", $DB +I::errstr, "\n"; if (defined ($dbhi)) { print "success: $dbhi \n"; return $dbhi; } else { print "Failure\n"; return -1; } } while (($dbh = connectdb($dwqa1,$depot,$pswd) eq -1) && ($sleepcount < + $sleeplimit)) { sleep ($sleepdur); ++$sleepcount; } my $sth = $dbh->prepare("select '$depot', t.table_name, column_name fr +om user_tables t, user_tab_columns c where c.table_name = t.table_nam +e"); $sth->execute();


Here is the output:
attempt to connect to database dbi:Oracle:xxxxx :IC_xxxxx 8:53a success: DBI::db=HASH(0x1c48904) Issuing rollback() for database handle being DESTROY'd without explicit disconne ct() at C:\Perl\dbitest.pl line 45.
Can't call method "prepare" without a package or object reference at C:\Perl\dbi test.pl line 51.

Is it that the object only exists inside the sub where it was created? If so, how do I make it live outside the sub?

In reply to DBI: pass $dbh from a sub by poqui

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.