First, it's Perl - not PERL. :) Second, LWP is GOOOOOD!

Moving on, consider the following CGI script, which i named simple.cgi:

#!/usr/bin/perl -Tw use strict; use CGI qw(:standard); print header; if (param('foo')) { print "you sent ", param('foo'); } else { print start_html,start_form,textfield('foo'), submit,end_form,end_html, ; }
The following LWP script will talk to it:
use strict; use LWP; use HTTP::Request::Common; my $url = 'http://localhost/cgi-bin/simple.cgi'; my $ua = LWP::UserAgent->new; my $request = POST($url, Content => [foo=>'hello world']); my $response = $ua->request($request); print $response->content, "\n";
Now, was that so bad? CGI.pm and LWP.pm are your friends. :) Use them! Just to show that i am a nice guy, here is an example that uses IO::Socket and GET to do roughly the same thing (oh, you were forgetting to read the results back, by the way):
use strict; use IO::Socket qw(:DEFAULT :crlf); $/ = CRLF . CRLF; my $sock = IO::Socket::INET->new( Proto => 'tcp', PeerAddr => 'localhost', PeerPort => 'http(80)', ); print $sock "GET /cgi-bin/simple.cgi?foo=hello%20world",$/; my $header = <$sock>; print $header; my $data; print $data while read($sock,$data,1024) > 0;
Much uglier in my opinion, and definitely less robust. If you plan to stick with raw sockets, then i suggest you buy and read Network Programming with Perl. Good luck! :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to (jeffa) Re: Simulating Form Posts from within Perl by jeffa
in thread Simulating Form Posts from within PERL by flappy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.