Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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 ( [id://11104177]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^5: $dbh could not be passed to function unless another variable is also passed
by Corion (Patriarch) on Aug 08, 2019 at 14:09 UTC
    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!

        I should quote only strings?

        Yes

        Anyway, what exactly does ‘The solution is to not quote variables when you don't need it.’ mean? That I should quote only strings?
        When you put quotes around something, you turn that thing into a string. You can quote whatever you want to, but, after you quote it, it will be a string, so you should only quote things that you want to be strings.

        But you should also remember what already is a string and avoid re-quoting existing strings, partly because it's wasted keystrokes, partly because it's potentially inefficient (it's less work to just use the existing string than to change the existing string into a new string and then use the new one), and partly because there could be some edge cases where quoting the existing string might change it or otherwise break something.

Re^5: $dbh could not be passed to function unless another variable is also passed
by Fletch (Bishop) on Aug 08, 2019 at 14:11 UTC

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-24 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found