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

Hi,

Just to solve a problem of a system command not executing in a perl script triggered thru mail.But works if it executed as a cgi or comman line
I just created a script that will just output the contents of the system command .I want to capture this contents in a perl script. How will i do it ? I just replicated some codes on net that doesnt work

use LWP::Simple; use LWP::UserAgent; use HTTP::Request; use HTTP::Response; use HTML::LinkExtor; $URL="http://www.perl.com/"; my $ua = LWP::UserAgent->new(); $content=$ua->get($URL); use Data::Dumper; print Dumper($content);
Can't locate object method "get" via package "LWP::UserAgent"

can any body suggest how to attain this?

20050728 Cleaned up by Corion: Removed PRE tags, put CODE and P tags in

Replies are listed 'Best First'.
Re: Getting contents of URL
by monarch (Priest) on Jul 28, 2005 at 04:43 UTC
    use LWP::UserAgent; sub get_url( $ ) { my ( $url ) = @_; my $ua = LWP::UserAgent->new(); # $ua->proxy('http','http://proxy.company.com:8080/'); $ua->timeout( 10 ); my $resp = $ua->get($url); if ( $resp->is_success ) { return( $resp->content ); } else { die( "LWP error: " . $resp->status_line . "\n" ); } } print( get_url( 'http://www.perl.com/' ) );
      I AM GETTING AN ERROR LIKE Can't locate object method "get" via package "LWP::UserAgent" at - line 11
        monarch's script works fine for me (it's basically the same as the example at LWP::UserAgent

        perhaps you forgot to make the new ua object?
        my $ua = LWP::UserAgent->new();

        "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: Getting contents of URL
by gopalr (Priest) on Jul 28, 2005 at 05:13 UTC

    Hi Sathish Kumar,

    You are missing this line in your coding

    my $ua=LWP::UserAgent->new;

    Do change your code like as follow and try again

    use LWP::UserAgent; $URL='http://www.perl.com/'; my $ua=LWP::UserAgent->new; $content=$ua->get($URL); use Data::Dumper; print Dumper($content);

    Thanks
    Gopal.R

Re: Getting contents of URL
by tulsyan (Beadle) on Jul 28, 2005 at 05:22 UTC

    Hello

    I think you are mising new() in your code

    my $ua=LWP::UserAgent->new;
Re: Getting contents of URL
by jbrugger (Parson) on Jul 28, 2005 at 04:45 UTC
    I don't understand your problem here, this script seems to work, but not if called by some mail trigger?
    Anyway, try to use warnings and strict, to get better feedback of the system.

    update:
    Ah, i see now, it's an update of the thread Executing system command. Why didn't you post it there?
    My advice using strict and warnings still stand, but as your script seems to work, the problem is probably not your script but the way your mailinglist is triggering (calling) your perl script.

    "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.
Re: Getting contents of URL
by sk (Curate) on Jul 28, 2005 at 05:15 UTC
    I guess this related to another post of yours Executing system command?

    (Referring to the other post)

    First of I would recommend that you update one post so that people can understand the whole picture instead of juggling between posts

    Next, providing more detailed description on what you have tried will be helpful. We understand your mailing list is not triggering your perl script but works from command line. But we will not have any clue as to why certain things are happening on your system unless we see error messages from your script directly. . It is hard to decide whether it is your mailing list app or perl scrip that is causing the problem here

    -SK

Re: Getting contents of URL
by ikegami (Patriarch) on Jul 28, 2005 at 06:03 UTC
    Your code works fine in LWP version 5.68. What version of LWP are you using?
    perl -le "use LWP; print LWP->VERSION"
    LWP::UserAgent's get method first appeared in LWP version 5.64.