CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced: Re: [cuboidgraphix] Help Parsing a line please.: Edit Log



FishMonger
Veteran / Moderator

Jan 15, 2009, 5:46 AM


Views: 6220
Re: [cuboidgraphix] Help Parsing a line please.

Kevin,
Good suggestion on using Text::ParseWords, I would not have thought of that, I was leaning towards using a regex.


cuboidgraphix,
I have a few additional suggestions.

Add error handling on @ARGV which produces a usage statement if the required parameter is missing.

Don't use DATA as your filehandle. DATA is a special built-in filehandle that is used to inline your data like Kevin did in his post. Instead you should be using a lexical var for the filehandle.

Drop the & from the quotewords(...) subroutine call. It's not needed and has side effects which you should learn about.

Instead of using the @words array, assign your vars directly in that same call to quotewords(...).

Use the DBI module as already recommended by Kevin and do the prepare statement prior to the while loop. This will increase efficiency.

Here's an updated version of a portion of the script which addresses most of those recommendations and a couple that I haven't mentioned.

Code
my $file = $ARGV[0] || die "Usage: $0 <filename>\n"; 
open my $FH, '<', $file or die "Can't open '$file' $!";

my $sth = $dbh->prepare("INSERT INTO $tablename VALUES (?,?,?,?,?,?,?,?,FALSE)");
while (<$FH>) {

next if $_ !~ /^#/;

my (undef, $index, $status, $activity, $datetime, $source, $severity, $faultname) = quotewords('\s+', 0, $_);
my ($time) = $datetime =~ /(\S+)$/;
my (undef,undef,undef,$day,$month,$year) = strptime($datetime);
my $date = sprintf("%4d-%02d-%02d",$year+2000,$month+1,$day);

$sth->execute($index, $status, $activity, $date, $time, $source, $severity, $faultname);
}



(This post was edited by FishMonger on Jan 15, 2009, 5:55 AM)


Edit Log:
Post edited by FishMonger (Veteran) on Jan 15, 2009, 5:55 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives