Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

formatting text prior to using it it to post to cgi...

by stevenrh (Beadle)
on Nov 17, 2004 at 22:15 UTC ( #408625=perlquestion: print w/replies, xml ) Need Help??

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:

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 '', [ip => "$_", email => '' ]); } 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 (Chancellor) 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...

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://408625]
Approved by NetWallah
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2023-05-29 17:45 GMT
Find Nodes?
    Voting Booth?

    No recent polls found