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

I'm trying to send a request to a web page and get the response back to use. This request is in a loop; I need to send one number at a time to the web page, let the analysis be done and get back the result. I have tried in vain to put the number in a variable and make this part of the request - what else can I do?

The code looks like this:

my $res = $ua->request(GET 'http://www.ncbi.nlm.nih.gov/entrez/viewer.fcgi?val=$BCnumber&db=Nucle +otide&dopt=xml&txt=on');

where $BCnumber is what I want to pass to the web site.
Thanks

Replies are listed 'Best First'.
Re: sending a request to a web page
by BrowserUk (Patriarch) on Oct 29, 2002 at 10:27 UTC

    Try this.

    my $res = $ua->request(GET "http://www.ncbi.nlm.nih.gov/entrez/viewer.fcgi?val=$BCnumber&db=Nucle +otide&dopt=xml&txt=on");

    In order to get your variables value substituted into the string, it needs to be interpolated. For this to happen, you need to indicate to Perl that you want it to do ths by using double-quotes rather than single quotes.


    Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
Re: sending a request to a web page
by mce (Curate) on Oct 29, 2002 at 10:52 UTC
    Hi,

    There are several ways, which can be devided into a GET and a POST.

    For simple requests, I prefer LWP::UserAgent.

    This is very simple:

    use LWP::UserAgent; my $url="http://www.ncbi.nlm.nih.gov/entrez/viewer.fcgi"; my $ua=LWP::UserAgent->new(); foreach my $i ( 0..10 ) { # create your own loop here my $rform= { val => $i, dba =>"Nucleotide", dopt =>"xml", txt =>"on", }; my $request=$ua->post($url, $rform); if ($request->is_success) { print $request->content; # do what you like here } else { print $request->error_as_HTML; } }
    I hope this helps
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium