unless (open(INPUT, "$inputfile")) { print "ERROR: Can't open file for reading : $!\n"; }
First, there's no need to quote $inputfile, just pass the scalar as the parameter to open. Second, although you check for an open error (good!) you don't do anything if it fails, aside from printing the error message. I would rewrite it using the following standard Perl idiom:
open INPUT, $inputfile or die "Can't open $inputfile for reading: $!";
Even better, you may want to use a more modern lexical filehandle instead of a global one, but this isn't a big deal for a small script, IMHO.
my @data = split("\t", $_); # assigning varables my $did1 = $data[0]; my $did2 = $data[1]; my $score1 = $data[2]; my $score2 = $data[3];
Here you put a bunch of stuff in @data just to take it out again. Instead, you can use a parallel assignment to make it more clear:
my ( $did1, $did2, $score1, $score2 ) = split "\t";
Note that I also ommitted the $_ from the split, since split works on $_ by default.
my $sth1 = $dbh->prepare("select d_id, c_id from d where d_id = '$did1';"); $sth1->execute(); my $sth2 = $dbh->prepare("select d_id, c_id from d where d_id = '$did2';"); $sth2->execute();
This is the biggest red flag for me. You're not using placeholders! What happens if someone sneaks some nasty SQL into $did1? Or if $did2 contains some characters that need escaping? You should always use DBI placeholders (see the DBI documentation for a complete explanation.)
my $sth1 = $dbh->prepare("select d_id, c_id from d where d_id = ?"); $sth1->execute( $did1 ); my $sth2 = $dbh->prepare("select d_id, c_id from d where d_id = ?"); $sth2->execute( $did2 );
Finally, I'm assuming you've got strict and warnings turned on but you just forgot to paste that, right? :) You are using lexical variables well and the code seems simple and straightforward.
In reply to Re: Feedback Appreciated on text-parsing, SQL querying subroutine
by friedo
in thread Feedback Appreciated on text-parsing, SQL querying subroutine
by Angharad
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |