Checking for undef after performing get()

Discussion in 'Perl Misc' started by stylechief, Jan 10, 2006.

  1. stylechief

    stylechief Guest

    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!
     
    stylechief, Jan 10, 2006
    #1
    1. Advertising

  2. stylechief wrote:
    > 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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 10, 2006
    #2
    1. Advertising

  3. stylechief

    Guest

    "stylechief" <> wrote:
    ....
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Jan 10, 2006
    #3
  4. stylechief

    Xicheng Guest

    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

    stylechief wrote:
    > 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!
     
    Xicheng, Jan 10, 2006
    #4
  5. stylechief

    Xicheng Guest

    Xicheng wrote:
    > 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..
    > # then do sth on $response->content.......
    > ==============================
     
    Xicheng, Jan 10, 2006
    #5
  6. stylechief

    stylechief Guest

    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.
     
    stylechief, Jan 10, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vikas Yadav
    Replies:
    0
    Views:
    1,146
    Vikas Yadav
    Sep 9, 2003
  2. Sachin

    #undef for function

    Sachin, Nov 25, 2005, in forum: C Programming
    Replies:
    3
    Views:
    432
    Peter Ammon
    Nov 28, 2005
  3. How to undef a typedef??

    , Nov 16, 2006, in forum: C Programming
    Replies:
    15
    Views:
    1,357
    Kenneth Brody
    Nov 18, 2006
  4. Klaus Ahrens
    Replies:
    6
    Views:
    393
    Alf P. Steinbach
    Nov 29, 2005
  5. Tim McDaniel

    undef($foo) versus $foo = undef()?

    Tim McDaniel, Aug 19, 2009, in forum: Perl Misc
    Replies:
    6
    Views:
    157
    Peter J. Holzer
    Aug 19, 2009
Loading...

Share This Page