Checking for undef after performing get()

S

stylechief

I am submitting some information directly from one Perl program located
on a local client to another Perl program located on a web server. I
need to verify that this information was successfully passed to the
Perl program located on a web server. The code works and submits
information successfully to the web server, but I have been unable to
verify this from the client side program. Example snippet of the
functioning code:
<code>
use strict;
use LWP::Simple;
use URI::URL;

my $url=url('http://www.webaddress.com/cgi-bin/XXX.pl');
$url->query_form(
name => $name,
address => $address,
email => $email,
reading => $reading,
);
get($url);
</code>
This submits the information and everything works fine on the web
server the way that it should. Now, the LWP::Simple docs say that the
get() function returns undef if it fails. I have been unable to find a
combination of code that works. Among them, after the above snippet:
<code>
if (!defined (get($url))){ print"Failed: $!\n$^E\n}
</code>
Does not work.
I am sure that this is simple, however, this is my first voyage into
the land of libwww-perl.
Please and thank you!
 
G

Gunnar Hjalmarsson

stylechief said:
I am submitting some information directly from one Perl program located
on a local client to another Perl program located on a web server. I
need to verify that this information was successfully passed to the
Perl program located on a web server. The code works and submits
information successfully to the web server, but I have been unable to
verify this from the client side program. Example snippet of the
functioning code:
<code>
use strict;
use LWP::Simple;
use URI::URL;

my $url=url('http://www.webaddress.com/cgi-bin/XXX.pl');
$url->query_form(
name => $name,
address => $address,
email => $email,
reading => $reading,
);
get($url);
</code>
This submits the information and everything works fine on the web
server the way that it should. Now, the LWP::Simple docs say that the
get() function returns undef if it fails. I have been unable to find a
combination of code that works. Among them, after the above snippet:
<code>
if (!defined (get($url))){ print"Failed: $!\n$^E\n}
</code>
Does not work.

_After_ the above? Wouldn't it make more sense to replace the
'get($url);' statement with that test, or else you would submit the
request twice, wouldn't you? (I'm assuming you don't care about what it
returns.)

Also, _how_ does it not work? "Does not work" is a useless problem
description.
 
X

xhoster

stylechief said:
This submits the information and everything works fine on the web
server the way that it should. Now, the LWP::Simple docs say that the
get() function returns undef if it fails. I have been unable to find a
combination of code that works. Among them, after the above snippet:
<code>
if (!defined (get($url))){ print"Failed: $!\n$^E\n}
</code>
Does not work.

What do you mean by "does not work"? What were you expecting? What
happened instead? If the server-side thing always works, then how is it
possible for you test whether your routine works? You can't test the body
of an "if" if the "if" condition is never true.
I am sure that this is simple, however, this is my first voyage into
the land of libwww-perl.

In general, this is not simple, at least if you need assurance in both
directions. If the web-server side finished OK, but the message it sends
back to the web-client script doesn't reach it, then you will think there
was an error on the web-server side when the wasn't.

Xho
 
X

Xicheng

For your case, it may be better to use LWP::UserAgent(Http::Response)
or WWW::Mechanize, both of them work like virtual browsers and can
check the server responses.
=========LWP::UserAgent=======
use strict;
use LWP;
use URI::URL;
my $url=url('http://www.webaddress.com/cgi-bin/XXX.pl');
$url->query_form(
name => $name,
address => $address,
email => $email,
reading => $reading,
);
my $browser = LWP::UserAgent->new;
my $response = $browser->get($url);
$response->is_success or die "wrong in getting $url : ",
$r->status_line;
# then do sth on $response->content.......
==============================
the use of WWW::Mechanize is very similiar but more powerful...
HTH,
Xicheng
 
X

Xicheng

Xicheng said:
For your case, it may be better to use LWP::UserAgent(Http::Response)
or WWW::Mechanize, both of them work like virtual browsers and can
check the server responses.
=========LWP::UserAgent=======
use strict;
use LWP;
use URI::URL;
my $url=url('http://www.webaddress.com/cgi-bin/XXX.pl');
$url->query_form(
name => $name,
address => $address,
email => $email,
reading => $reading,
);
my $browser = LWP::UserAgent->new;
my $response = $browser->get($url);
$response->is_success or die "wrong in getting $url : ",
$r->status_line;
$response->status_line;

Sorry for the above typo..
 
S

stylechief

Xicheng, thank you for your helpful response. Your method does make
the web server perform its function, but I think that I need to code my
web based script so that it provides some kind of response when
activated, otherwise, I get a 500 Internal Server Error, even though
the script worked fine. Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top