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

Monks- I'm trying to craft a program to retrive some data from a website, print it to STDOUT, and then wait for the response from the user to go and make a second request. I can get the initial response back just fine, but the program just ends (after print $resp;) and returns the command prompt. How can I get it to wait for the user response? (I've typed a query line after the print $resp line but that just prints to screen, rather than waiting for the response.) Many thanks.
#! /usr/bin/env perl use strict; use URI::Escape; use LWP::UserAgent; #my $outfile = shift; my $date1 = shift; my $date2 = shift; my $url = "http://maewest.gso.uri.edu/cgi-bin/nph-jg/htnfronts.ascii?t +ime"; # Save page to file my $ua = init_lwp(); my $uri = create_query($url, $date1, $date2); my $resp = execute_query($ua, $uri); chomp $resp; print $resp; # print list to screen of available dates. print "Choose a date and enter it on the command line: "; sub init_lwp { my $ua = LWP::UserAgent->new; $ua->env_proxy(); return $ua; } sub create_query { my($base_url, $dt1, $dt2) = @_; my $url = $base_url; if (defined($dt2)) { $url .= "&time>=$dt1&time<=$dt2"; } #else { # $url .= "?time=$dt1"; #} my $uri = URI->new($url); # Force unescape to prevent unwanted escapes of < and > ${$uri} = uri_unescape(${$uri}); return $uri; } sub execute_query { my($ua, $uri, $file) = @_; my $request = HTTP::Request->new('GET', $uri); my $response = $ua->request($request); return $response->as_string(); }

Replies are listed 'Best First'.
Re: response in LWP::UserAgent
by Zaxo (Archbishop) on Jul 27, 2001 at 00:45 UTC

    Your program ends with printing the prompt line. You nead to read a response from the user and then do what you wish with the result.

    $|=1; print "Choose a date and enter it on the command line: "; my $user_response; until ($user_response = <STDIN>){}; # do something with $user_response

    After Compline,
    Zaxo

    Update: improved it with autoflush, until and diamond
      Problem solved. Many thanks for the suggestion.
      I know I need to read the STDIN, but I can't get the program to wait. It just instantly returns the prompt.
Re: response in LWP::UserAgent
by da (Friar) on Jul 27, 2001 at 00:52 UTC
    All you need is to read a line from the <STDIN> filehandle (or the default filehandle <>, if it hasn't been reassigned).

    print "Give me an answer:"; $answer = <STDIN>; chomp $answer; print "You said '$answer'.\n";
    See this node for a little more info. (Which you could find with a site search for 'input')

    ___ -DA > perl -MPOSIX -le '$ENV{TZ}="EST";print ctime(1000000000)' Sat Sep 8 20:46:40 2001