Re: Perl script doesnt behave well
- From: luke_devon@xxxxxxxxx (Luke Devon)
- Date: Sun, 20 Jul 2008 23:14:04 -0700 (PDT)
Dear Friends.
This is the script that running in for squid redirector program. From the list I got lots of help to properly coding it. But unfortunately that problem is stil there . Problem is after some times ( After 8-10 hours) , that redirect url doesn't functioning properly. Getting hang or crached. Meanwhile i checked the database status also .database is up and running.
Can somebody help me please ? where is the problem ? in the logic of the code or some where else ?
#!/usr/bin/perl
use DBI;
use strict;
use warnings;
## no buffered output, auto flush
$|=1;
my $dbh = DBI->connect("dbi:mysql:#####;localhost","####","####") || "Error Opening DataBase: $DBI::errstr\n";
if (!$dbh->err()) {
while (<STDIN>) {
chomp;
my ($url, $x, $####) = split;
($###) = $### =~ /([0-9.]+)/;
my $query = "SELECT * from ###### where ###### = '" . $#### ."' order by ###### desc";
my $sth = $dbh->prepare($query);
$sth->execute() or die "Unable to call execute $!\n\n";
my ($####, $#####);
next unless my $ref = $sth->fetchrow_hashref;
$##### = $ref->{'#####'};
$#### = $ref->{'####};
if (!($url =~ m#######)) {
if ($url =~ m#\?#) {
$url .= "&#####=" . $##### . "-" .. $#### . "-" . $#####;
} else {
$url .= "?#####=" . $##### . "-" . $### . "-" . $#####;
}
print $url."\n";
} else {
print "\n";
}
}
}else {
print "\n";
}
my $sth->finish();
$dbh->disconnect();
Thank you
Luke.
----- Original Message ----
From: Rob Dixon <rob.dixon@xxxxxxx>
To: Perl <beginners@xxxxxxxx>
Cc: luke devon <luke_devon@xxxxxxxxx>
Sent: Monday, July 14, 2008 23:27:21
Subject: Re: Perl script doesnt behave well
luke devon wrote:
I am using perl script to handle some function of squid redirector program .
Actually its working fine. But after some time , that functions goes off.
That's meant VALUE-A doesnt comes in to the request.
I checked the DB , it also fine.
CPU also nothing
Can some body help me please ?
#!/usr/bin/perl
use DBI;
use strict;
use warnings;
# no buffered output, auto flush
$|=1;
my ($dbh,$sth,$dbargs,$VALUE-A,$VALUE-B,$query);
It is best to declare variables at their point of first use, unless a greater
scope is necessary, as for $VALUE-A and $VALUE-B, for which see below.
$dbh = DBI->connect("dbi:mysql:List;localhost","root","") || "Error Opening DataBase: $DBI::errstr\n";
my $dbh = DBI->connect("dbi:mysql:List;localhost","root","") or die "Error
Opening DataBase: $DBI::errstr"
if (!$dbh->err()) {
If you test the success of the connect, above, then this test will always
succeed and there is no need for it.. In any case if there is a problem then $dbh
will not be a valid database handle and so won't have an err method.
while (<STDIN>) {
chomp;
my ($url, $x, $ip) = split(/ /);
my ($url, $x, $ip) = split;
is better.
$ip = substr($ip, 0, (length($ip)-2));
Having read your previous posts about how to extract an IP address from a
string, I believe this should be written
($ip) = $ip =~ /([0-9.]+)/;
and may be the source of your problem if there are not always two characters of
junk data after the IP address.
$query = "SELECT * from ACCOUNT where someField = '" . $ip ."' order by xxx_yyy desc";
$sth = $dbh->prepare($query);
$sth->execute();
my $query = "SELECT * from ACCOUNT where someField = ? order by xxx_yyy desc";
my $sth = $dbh->prepare($query) or die $dbh->errstr;
$sth->execute($ip) or die $dbh->errstr;
The scalar variables $VALUE-A and $VALUE-B should also be declared here, and
ideally have more descriptive names.
my ($VALUE-A, $VALUE-B);
if (my $ref = $sth->fetchrow_hashref()) {
$VALUE-A = $ref->{'CallingStationId'};
$VALUE-B = $ref->{'AcctSessionId'};
}else{
$VALUE-A = "NA";
}
At this point you may have a value of 'NA' for $VALUE-A and $VALUE-B could be
either undefined or, worse, have a value left over from the previous execution
of the read loop. I think you should just write
next unless my $ref = $sth->fetchrow_hashref;
$VALUE-A = $ref->{'CallingStationId'};
$VALUE-B = $ref->{'AcctSessionId'};
if (!($url =~ m#xxxyyyy#)) {
if ($url =~ m#\?#) {
$url .= "&xxxyyyy=" . $VALUE-A . "-" . $ip . "-" . $VALUE-B;
} else {
$url .= "?xxxyyyy=" . $VALUE-A . "-" . $ip . "-" . $VALUE-B;
}
print $url."\n";
} else {
print "\n";
}
}
}else {
print "\n";
It seems appropriate to do something more than just print a blank line if your
connection to the database failed.
}
$sth->finish();
$dbh->commit();
$dbh->disconnect();
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/
Send instant messages to your online friends http://uk.messenger.yahoo.com
- Prev by Date: how to convert data available from excel to hash?
- Next by Date: Re: how to convert data available from excel to hash?
- Previous by thread: Re: Perl script doesnt behave well
- Next by thread: how to read the formatted data from the file?
- Index(es):
Relevant Pages
|