Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: perl and psql

by gogoglou (Beadle)
on Dec 02, 2009 at 14:14 UTC ( [id://810566]=note: print w/replies, xml ) Need Help??


in reply to Re: perl and psql
in thread perl and psql

thanks for your reply. I am really new in perl soany help is usefull. I get a small error message though. first I create at he top of the script my $table which has a start and an end position, but then when I replace the print with the example you gave me I get an error that says that my clusters requires explicit package name. I don't really understand why this happens in this case, since the clusters have been declared. Any ideas would be really usefull. Thanks

Replies are listed 'Best First'.
Re^3: perl and psql
by bv (Friar) on Dec 02, 2009 at 14:29 UTC

    There was a bug in the code that was posted as a reply. I don't think Xilman intended that code to be blindly copy-and-pasted, but here's what's probably going on:

    # This should not have the '$dbh->' at the beginning, but the real iss +ue is # $cluster here is a scalar, but foreach my $cluster (@clusters) { # here, they are being treated as arrays (if they were interpolated as + written. # $dbh->do ("INSERT INTO $table (start, end) VALUES (cluster[0], clust +er[1]);"); # a better approach, if your DBI module supports placeholders: $dbh->do ("INSERT INTO $table (start, end) VALUES (?,?);", @{$cluster} +) # or even: $dbh->do ("INSERT INTO $table (start, end) VALUES (?,?);", +$cluster->[0], $cluster->[1]) }

    Again, this is untested code, and may not work as advertised. If the -> and @{} are confusing you, check out perlreftut for more info on references.


    @_=qw; Just another Perl hacker,; ;$_=q=print "@_"= and eval;
      $dbh->do ("INSERT INTO $table (start, end) VALUES (?,?);", @{$cluster})

      When you use do with placeholders and bind values, you also need to specify the \%attr hashref (for syntactical reasons, because the synopsis is $dbh->do($statement, \%attr, @bind_values)).  You may use undef if you don't have any attributes.

      $dbh->do ("INSERT INTO $table (start, end) VALUES (?,?)", undef, @$clu +ster)

      Thank you. It was an unfortunate cut&paste error on my part.

      I'm well aware, of course, that place holders are a Good Idea(tm). The original Perl script from which I made the c&p used them but I removed them from the posted snippet to simplify it.

      As I said originally, the OP should take my fragment as a hint for a learning exercise and not as a complete answer.

      Paul

Log In?
Username:
Password:

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

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

    No recent polls found