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

Hi Guys, hopefully someone out there can give me a hand with this. I have created a small perl script that checks the port and some content for a dynamic web server. If either is not available, it generates an alert for our support staff. Here's my problem, the dynamic server is not sending me the information quickly enough, and regardless of it's status the script assumes it's not available. see snippet.
sub GetWebLogic { $url = "http://" . $server . ":" . $port . "/HttpController?GetPag +e=UserLogonForm"; if ($Testing eq "Yes" ) { print "Checking Dynamic Page\n"; } $ua = new LWP::UserAgent; $request = HTTP::Request->new(GET => $url); $response = $ua->request($request); if ($response->is_success) { $dynamic = $response->content; } @dynamic = split(/\n/,$dynamic); foreach $line (@dynamic) { ## Check the output of the web page for the HTML <HEAD> tag if ($line =~ /<HEAD>/i) { if ($Testing eq "Yes") { print "Found the Dynamic HTML <HE +AD> tag\n"; } $DynamicPageCheckOK = "Yes"; } if ($DynamicPageCheckOK eq "Yes") { ## Page returned do noting } else { ## Page didn't return generate an error &GenerateServerError; } } }
The script returns all the page information, but I look for the HEAD tag, the script continues on its way before receiving all the page, if I change it the look for the HTML tag it works fine. So after this long winded description, my question would be, how do I have my script wait until all the input is received back. TIA liquidc00l

Replies are listed 'Best First'.
Re: Waiting for a response
by perrin (Chancellor) on Apr 10, 2002 at 19:33 UTC
    You can pass a "timeout" value to LWP::UserAgent->new(). See LWP::UserAgent. Or else you could rewrite that lame WebLogic Java code so it isn't so slow.
Re: Waiting for a response
by wardk (Deacon) on Apr 10, 2002 at 19:35 UTC
    Try testing the $DynamicPageCheckOK outside the loop. (see comments in your code below "MOVE THIS" "HERE")

    You may also consider exiting the foreach right after finding the HEAD tag to avoid looping after you know the answer.

    sub GetWebLogic { $url = "http://" . $server . ":" . $port . "/HttpController?Get +Page= +UserLogonForm"; if ($Testing eq "Yes" ) { print "Checking Dynamic Page\n"; } $ua = new LWP::UserAgent; $request = HTTP::Request->new(GET => $url); $response = $ua->request($request); if ($response->is_success) { $dynamic = $response->content; } @dynamic = split(/\n/,$dynamic); foreach $line (@dynamic) { ## Check the output of the web page for the HTML <HEAD> tag if ($line =~ /<HEAD>/i) { if ($Testing eq "Yes") { print "Found the Dynamic HTML +<HEAD +> tag\n"; } $DynamicPageCheckOK = "Yes"; } } # HERE if ($DynamicPageCheckOK eq "Yes") { ## Page returned do noting } else { ## Page didn't return generate an error &GenerateServerError; } # } # MOVE THIS }
    UPDATE: Note that if ($response->is_success) is true you successfuly "got" the requested page, so unless you need some to inspect the content to take some action, you probably don't really need to take the effort to through the content. (also consider what happens if they go and make a change and lowercase the HEAD tag on you :)
      if ($line =~ /<HEAD>/i) {

      also consider what happens if they go and make a change and lowercase the HEAD tag on you :)

      Hmm. What does the /i do to a regex? perlman:perlre

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        just extending my streak of misreading posted code...