Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by tukusejssirs (Beadle)
on Aug 08, 2019 at 13:14 UTC ( [id://11104173]=note: print w/replies, xml ) Need Help??


in reply to Re: $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

Indeed it works. Thank you. But can you ten me, why my $dbh = $_[0] does not work?
  • Comment on Re^2: $dbh could not be passed to function unless another variable is also passed
  • Download Code

Replies are listed 'Best First'.
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

    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 $

      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];

        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://11104173]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-20 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found