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

Hello Gurus!
I am not new to Perl, however, pretty new to this module LWP::UserAgent, which I recently started using to send a http server and based on the request execute a proc in the database. Well, it didn't work. After a lot of searching through the code, I tried doing this:
use DBI; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $r = $ua->post('http://localhost/'); my $dbh = DBI->connect("dbi:Sybase:server=$server", '$uid', '$password +') || die "Bad db server, no donuts!"; my $sql = "declare \@rc int exec \@rc = myProcName " . "select \@rc"; $row = $dbh->selectall_arrayref($sql); print $row->[0][0], "\n";
It fell into an infinite wait until I manually stopped execution by Ctrl-C. However, commenting out the line of the post request, the script runs successfully... whats going wrong here? Can anyone please suggest what's the problem here and if there are any workarounds...

I am using ActivePerl 5.6 on Windows.

Replies are listed 'Best First'.
Re: using LWP::UserAgent and DBI together
by pajout (Curate) on Nov 09, 2009 at 15:15 UTC
    I guess your http server is not responding the request made by $ua and $ua just waits for that response.

    Sourav, you should primarily decide, what do you want - if your goal is direct connection with some sql database engine (rdbms), DBI and proper DBD:: driver is a good choice. You can connect local rdbms (the software running on the same computer as your perl code), or you can connect remote rdbms, typically via pure tcp/ip protocol.

    But if you want to establish communication with http server (local or remote, which is evident from url), you can use LWP::UserAgent and related modules.

    Update: I apologize if I did not understand you correctly, perhaps you want to do both, http request and some sql command...

      That's right, I'd like to use both a http request and a database connection. My main requirement is - send a post request to a http server and see what the response is, if it's a success, then run a stored proc at the backend to update a table with the response from the http server. The code I posted is a simplfied form of the actual code, which curently waits infinitely during the $dbh->selectall_arrayref() execution (and not the $ua = LWP::UserAgent->new() call). I guess there are some problems when you are using both these two modules in a single script, however, I could not find any specific document describing the source of the problem or anything to solve it (that is, to have a http request and a dbi call in the same script). Any help?
        One more thing I might add... I tried replicating the same problem with ActivePerl 5.8, here it seems to be working fine without a glitch... but I can't use this version at work (that's only 5.6 there)....