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();
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.