how to use default_headers?

T

Tim Jowers

Advice? I get this error message:

C:\houses\scraper>perl test2.pl
Can't locate object method "default_headers" via package
"LWP::UserAgent" at tes
t2.pl line 11.

But downloaded the latest libwww-perl-5.801 and copied the files from
/bin and /lib into my Perl install. Why does not this work?
default_headers are a new method added this year according to the post
http://groups.google.com/[email protected]&rnum=2

Here's my program. It's a test program as referrer must be set in the
real program.

# Create a user agent object
use LWP::UserAgent;
$ua = LWP::UserAgent->new(default_headers);
$ua->agent("MyApp/0.1 ");

# Create a request
my $req = HTTP::Request->new(POST =>
'http://search.cpan.org/search');
$req->content_type('application/x-www-form-urlencoded');
$req->content('query=libwww-perl&mode=dist');

my $h = $ua->default_headers;
$h->header('Referer' => shift);

# Pass request to the user agent and get a response back
my $res = $ua->request($req);

# Check the outcome of the response
if ($res->is_success) {
print $res->content;
}
else {
print $res->status_line, "\n";
}

TIA,
TimJowers
 
A

A. Sinan Unur

(e-mail address removed) (Tim Jowers) wrote in
Advice? I get this error message:

C:\houses\scraper>perl test2.pl
Can't locate object method "default_headers" via package
"LWP::UserAgent" at tes
t2.pl line 11.

But downloaded the latest libwww-perl-5.801 and copied the files from
/bin and /lib into my Perl install. Why does not this work?

Because you can't just make s**t up and expect it to work.

If you are on Windows, use ppm to install packages.
 
T

Tim Jowers

Bob Walton said:
Tim said:
Advice? I get this error message:

C:\houses\scraper>perl test2.pl
Can't locate object method "default_headers" via package
"LWP::UserAgent" at tes
t2.pl line 11.

What makes you think "default_headers" is an object method? The docs
say it is a hash key of an optional hash which may be provided as an
argument to LWP::UserAgent->new() in order to modify its behavior. So
an appropriate usage might be something like [untested]:

my $ua=LWP::UserAgent->new({default_headers=>...});
But downloaded the latest libwww-perl-5.801 and copied the files from
/bin and /lib into my Perl install. Why does not this work?

Maybe you missed something the install does? Why didn't you just do a
proper install?

...
TimJowers

Thanks for responding. It was apparently a port problem as it worked
fine in U**x but I was running under ActiveState ActivePerl in
Windows. Haven't done any Perl since 1994 so I've switched out most
anything I knew 'bout it. Of course I did a fresh install et cetera -
the "object method" is the error message received from perl, not my
concoction. Got another cut that sets the header elsewhere rather than
using default_headers and it worked like a charm.

Best luck, TimJowers
 

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,981
Messages
2,570,187
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top