develooper Front page | perl.libwww | Postings from October 2001

Re: HTTP::Request/LWP::UserAgent: error: only occurs within class context

Thread Previous
From:
Darren R. C. KELLY
Date:
October 18, 2001 16:32
Subject:
Re: HTTP::Request/LWP::UserAgent: error: only occurs within class context
Message ID:
3BCF66BC.4A99C3EA@webel.com.au
Dear Darren,

This is quite a curious problem.
It is shame nobody has responded yet.
Good luck and keep trying,

regards,

Darren.

"Darren R. C. KELLY" wrote:
> 
> Hi libwww-ers,
> 
> I encapsulated a request-and-parse sequence for HTML into a 'fetch' sub.
> It works fine as a 'sub', but fails as a class member.
> Even stranger, it DOES work perfectly when I run it through the debugger
> perl -d.
> Something strange about using it in a package context is messing it up.
> 
> The HTML returned as error when it fails is:
> 
> <html>
> <head><title>An Error Occurred</title></head><body>
> <h1>An Error Occurred</h1>
> 
> 500 Can't locate object method &quot;blocking&quot; via package
> &quot;IO::Socket::INET&quot;
> (perhaps you forgot to load &quot;IO::Socket::INET&quot;?)
> 
> </body>
> </html>
> 
> (otherwise the requested HTML page is successfully returned).
> 
> I just upgraded both perl and LWP to latest versions; until then I was
> getting the message
> 
> ...
> 500 Broken pipe
> ...
> 
> I tried including a 'use IO::Socket::INET'; made no difference.
> 
> Script (works fine)
> ---
> #!/usr/bin/perl -w
> use LWP::UserAgent;
> use HTTP::Request;
> 
> print fetch('http://www.perl.org');
> 
> sub fetch {
>   my $url = shift;
>   my $ua = LWP::UserAgent->new;
>   my $request = HTTP::Request->new(GET => $url);
>   my $response = $ua->request($request);
>   my $html;
>   if ($response->is_success) {
>      $html = $response->content;
>   } else {
>     $html = $response->error_as_HTML;
>   }
>   return $html;
> }
> ---
> 
> Encapsulated in package:
> ---
> package El::HTMLrequest;
> #*************************************************************
> # $Id: HTMLrequest.pm, v 1.00 Sat Sep 22 14:08:46 2001 drck $
> #*************************************************************
> 
> use strict;
> 
> use vars qw(@ISA $VERSION);
> $VERSION = sprintf("%d.%02d", q$Revision: 1.00 $ =~ /(\d+)\.(\d+)/);
> sub VERSION {$VERSION;}
> 
> use LWP::UserAgent;
> use HTTP::Request;
> use HTML::TreeBuilder;
> 
> sub fetch {
>   my ($self, $url) = @_;
> 
>   my $ua = LWP::UserAgent->new;
>   my $request = HTTP::Request->new(GET => $url);
>   my $response = $ua->request($request);
>   my $html;
>   if ($response->is_success) {
>     $html = $response->content;
>   } else {
>     $html = $response->error_as_HTML;
>   }
>   my $tree = new HTML::TreeBuilder;
>   $tree->parse($html);
>   return $tree;
> }
> 
> sub new {
> 
>   my ($type,%p) = @_;
>   my $class = ref($type) || $type;
>   my $self = {};
>   bless ($self, $class);
> 
>   return $self;
> }
> 
> 1;
> ---
> 
> Script for package.
> Fails outside debugger.
> Works perfectly under debugger !
> ---
> #!/usr/bin/perl -w -I../lib
> use El::HTMLrequest;
> my $r = new El::HTMLrequest;
> my $html = $r->fetch('http://www.perl.org');
> print $html->as_HTML;
> ---
> 
> very glad for help,
> 
> Darren Kelly
> 
> http://www.webel.com.au (Elements of the Web)

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About