http://qs1969.pair.com?node_id=408625

stevenrh has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

I have a script that posts to a cgi, using a file i downloaded and awk'd as input. I currently would like to eliminate the wak step, and use oerl instead.
the file comes with three(3)fields:

IP:aa:hex_value
so I have this command:
grep ^192.168. file | awk F: '(print $1)' | sort > newfile

then run my LWP/POST script

the following code was an attempt to get that fist column, and use it as my input.....all with perl, not the shell.

#!/usr/bin/perl -w $|++; use HTTP::Request::Common; use LWP::UserAgent; $ua = LWP::UserAgent->new; my $file = 'fulldata'; open(FILE,"fulldata") || die "Could not open $file: $!"; while (<FILE>) { ($ip) = (split /:/) [0]; if (/^192.168./) { chomp; $ua->request (POST 'http://domain.org/subgmit.cgi', [ip => "$_", email => 'the_dude@dommain.net' ]); } last } close(INFO) or die "Error writing $file: $!";

Don't hold back, any (even brutal)advice is welcome...:() Thanks -stevenrh-

Replies are listed 'Best First'.
Re: formatting text prior to using it it to post to cgi...
by ikegami (Patriarch) on Nov 17, 2004 at 22:44 UTC

    Your indenting "needs work".

    Don't you mean open(FILE, $file) instead of open(FILE,"fulldata")?

    Why don't you move the chomp right below the while (<FILE>)? Actually, you don't need the chomp at all since you discard the end of the line.

    Your if should be checking $ip, but it's checking $_.

    /^192.168./ matches '19231681'. You want /^192\.168\./.

    You don't need the split.
    ($ip) = (split /:/)[0]; if ($ip =~ /^192.168./) {
    can be replaced with
    if (/^(192\.168\.\d{1,3}\.\d{1,3}):/) { $ip = $1;.

    You're closing INFO, yet your handle is FILE.

    You don't check the results of request

    Your original code sorted the results, but your Perl script doesn't.

Re: formatting text prior to using it it to post to cgi...
by jdporter (Paladin) on Nov 18, 2004 at 02:19 UTC
    I would like to eliminate the awk step, and use perl instead.
    Why? I mean, if it's just for learning purposes, fine. But you have a solution that ain't broke...