in reply to Use of unitialized variable?

(note: I didn't write the initial code.) Thanks for the suggestions, and the fix for the warning. :) I'll probably re-write it all, the entire tool set (someday), which is used to index web pages, check broken links, etc. But that's too big of a project for the here and now. :( Just trying to fix the bugs, and memory leaks(!) at the moment. Unless someone knows of a web site crawler that will catch things like missing mouseovers, example:
<script language=javascript><!-- if (document.images) { navigation = new makeimgarray(9) navigation_mo = new makeimgarray(9); navigation[1].src = "/pool/images/psyche//butt_index.gif"; navigation_mo[1].src = "/pool/images/psyche//butt_index.mo.gif"; ... </script>
No link checker I've found will check for the /pool/images/psyche//butt_index.gif image, except for the spagetti I'm having to work with right now. :) Anyhow, here's the changes I made to the gethtml subroutine for your critique:
#/usr/bin/perl -w #gethtml.pl : HTML fetch and return. 28 AUG 2000 use Socket; use strict; sub gethtml { my $my_url = ($_[0]); my $my_host=''; my $my_request=''; my $my_html_return=''; ($my_host, $my_url) = ($my_url =~ m#(.*?)/(.*)#); $my_request = "GET \/$my_url HTTP/1.0\nAccept: */*\nHost: $my_host\nUs +er-Agent: WebMangle/1.0\n\n"; socket(MY_SOCKET, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || retu +rn "Error: $!"; connect(MY_SOCKET, sockaddr_in(80,inet_aton($my_host))) || return "Err +or: $!"; send(MY_SOCKET, $my_request, 0x0); while(<MY_SOCKET>) {$my_html_return .= $_;} close MY_SOCKET || return "Error: $!"; return $my_html_return; } 1;

Replies are listed 'Best First'.
(jeffa) RE: Re: Use of unitialized variable?
by jeffa (Bishop) on Aug 29, 2000 at 05:37 UTC
    Actually, you will save yourself a lot of trouble by using LWP and HTTP::Request, like so:
    use strict; use LWP::UserAgent; use HTTP::Request::Common; print &getHTML('http://www.perlmonks.org'); sub getHTML($) { # one $ means one scalar arg - for us humans my $url = shift; #create a new agent and name it my $ua = new LWP::UserAgent; $ua->agent("Shmozilla/0.1 " . $ua->agent); #create new request my $req = new HTTP::Request GET => $url; #send the and try to get a response my $resp = $ua->request($req); return ($resp->is_success) ? $resp->content : "Error\n"; }
        Yep, you are right - in my haste I overlooked that each call to sub getHTML instantiates a new agent.
        What a waste!! :0

        One more time:

        use strict; use LWP::Simple; print &getHTML('http://www.perlmonks.org'); sub getHTML($) { my $url = shift; my $content = get($url); return getprint($url); }