Dear Monks,

I'm trying to send a GET request to localhost in an attempt to fetch a perl script in my Apache cgi-bin directory.

Here is what I have for the following variables:

my $host = 'localhost'; my $path = '/cgi-bin/second.pl'; my $port = 80;

If I use the short form of IO::Socket::INET->new() and create a socket like this:

my $CONN = IO::Socket::INET->new("$host:$port") or die "Couldn't connect: $!";

I am able to send a request:

my $req = "GET $path HTTP/1.0 $CRLF$CRLF"; $CONN->syswrite($req);

...and then I can read the header that the server sends back just fine:

$/ = CRLF . CRLF; #To read the whole header as one 'line' binmode $CONN; #Turn off \n conversion for getline() in next line my $header = $CONN->getline; $header =~ s/$CRLF/\n/g; say $header; --output:-- HTTP/1.1 200 OK Date: Sun, 21 Feb 2010 12:28:24 GMT Server: Apache/2.2.4 (Unix) mod_ssl/2.2.4 OpenSSL/0.9.7l DAV/2 PHP/5.2 +.3 mod_python/3.3.1 Python/2.4.4 Content-Length: 294 Connection: close Content-Type: text/html

However, if I keep everything the same, and I try to create the socket using the long format:

my $CONN = IO::Socket::INET->new( Peerhost => $host, Peerport => 'http(80)', ) or die "Couldn't connect: $!";

I get the following errors:

Use of uninitialized value $header in substitution (s///) at 4perl.pl +line 55. Use of uninitialized value $header in say at 4perl.pl line 56.

I also tried using just 80 for the Peerport, and I get the same result. Can anyone tell me why I am unable to retrieve the header using the long format for IO::Socket::INET->new()?

Thanks

Here's the full program:

use strict; use warnings; use 5.010; use IO::Socket qw{ :DEFAULT :crlf }; my $host = 'localhost'; my $path = '/cgi-bin/second.pl'; my $port = 80; =pod my $CONN = IO::Socket::INET->new("$host:$port") or die "Couldn't connect: $!"; =cut my $CONN = IO::Socket::INET->new( Peerhost => $host, Peerport => 'http(80)', ) or die "Couldn't connect: $!"; my $req = "GET $path HTTP/1.0 $CRLF$CRLF"; $CONN->syswrite($req); $/ = CRLF . CRLF; #To read whole header as one 'line' binmode $CONN; #turn off \n conversion for getline() below my $header = $CONN->getline; $header =~ s/$CRLF/\n/g; say $header; $CONN->close;

In reply to IO::Socket::INET->new(): short form v. long form by 7stud

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.