Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: $dbh could not be passed to function unless another variable is also passed

by hippo (Bishop)
on Aug 08, 2019 at 13:23 UTC ( [id://11104175]=note: print w/replies, xml ) Need Help??


in reply to Re^2: $dbh could not be passed to function unless another variable is also passed
in thread $dbh could not be passed to function unless another variable is also passed

That also works fine. SSCCE:

$ cat close.pl #!/usr/bin/env perl use strict; use warnings; use DBI; my $dbh = DBI->connect ('dbi:SQLite:dbname=foo', '', ''); close_db ($dbh); sub close_db { my $dbh = $_[0]; $dbh->disconnect or die "Could not disconnect: $DBI::errstr\n"; } $ perl close.pl $
  • Comment on Re^3: $dbh could not be passed to function unless another variable is also passed
  • Download Code

Replies are listed 'Best First'.
Re^4: $dbh could not be passed to function unless another variable is also passed
by tukusejssirs (Beadle) on Aug 08, 2019 at 14:01 UTC

    Probably I still miss something.

    I have another function called create_table and the db handle does not pass correctly (should I bless it?). I does work when I create the handle within the function.

    create_table($dbh, $table, "record_date char(20)"); sub create_table { use DBI; # Variables my $sth; # Database handle my $dbh = "$_[0]"; # Table name and optionally schema name (schema.table) my $table = "$_[1]"; # Columns and their type my $cols_type = "$_[2]"; # } else { # die RED, "ERROR: You have to supply database handle, table n +ame, the columns list with their type and if you to disconnect the da +tabase connection.\n Stopped$!"; # } # Create table $sth = $dbh->prepare("create table $table ($cols_type);"); $sth->execute(); print "INFO: The table has been created successfully.\n"; return; }
      my $dbh = "$_[0]";

      What is in $dbh and why do you force it to a string?

      Also, what is the error message? It is most likely something like Can't call method prepare on DBD::foo::dbh, but telling us the exact error message helps us much better diagnose your situation.

      The solution is to not quote variables when you don't need it. Remove the double quotes:

      my $dbh = $_[0];

        Corion: The error was Can't locate object method "prepare" via package "DBI::db=HASH(0x215bc78)" (perhaps you forgot to load "DBI::db=HASH(0x215bc78)"?)

        Fletch and Corion:

        You are right. When I removed the quotes (leaving only my $dbh = $_[0];), it works as expected.

        Anyway, what exactly does ‘The solution is to not quote variables when you don't need it.’ mean? That I should quote only strings?

        Thank you both!

      You've stringified all three of your arguments which isn't what you want to do. A DBI handle is an object which is (usually) implemented as a reference to a data structure such as a hash. When you pass it through double quotes you're breaking that reference (hand waving a bit here; see perlobj for more details) so when you try and call methods upon it you're trying to call methods on a string value (hence your error).

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11104175]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (None)
    As of 2024-04-19 00:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found