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

silly mistake...see responses

For some reason I do not understand, in the following piece of code, "$r" is a hash reference. This is an incomplete new function from a .pm file. With the exact same piece of code used outside of a function in a script, "$r" would be a string (the _content from the reply hash), which is what I've expected from LWP::UserAgent up to now, and what the documentation describes.
use LWP::UserAgent; use HTTP::Cookies; sub new { my $self = {}; my $apiphp = "http://localhost/wiki/api.php"; my $header = HTTP::Headers->new(Content_Type => "application/x-www +-form-urlencoded"); my $cjar = HTTP::Cookies->new(file => "/root/perl/mediawiki/cookie +s/mediawiki.cookies", autosave => 1); my $client = LWP::UserAgent->new(default_headers => $header, cooki +e_jar => $cjar) || die "MWUser.pm LWP::UserAgent failed!\n"; my %login = ( 'action' => 'login', 'format' => 'xml', 'lgname' => 'user', 'lgpassword' => 'pass' ); my $r = $client->post($apiphp, Content=>\%login); while (my ($k, $v) = each (%$r)) { print "$k = $v\n"; } }
I've been looking thru the docs for the various HTTP base classes here for a way to set this behaviour but haven't found anything yet.

I'd rather not have to bother with selecting _content from a hashref, I was quite happy with the string. Does anyone know what's up?

Replies are listed 'Best First'.
Re: LWP::UserAgent returns hash ref with post
by runrig (Abbot) on Apr 22, 2010 at 14:37 UTC
    Which documentation are you looking at? $r is a response object, look at the docs to see how to get the content from it. Maybe you were using LWP::Simple?
      Yeah, silly me, see the above response for my screw-up. Sheesh.
Re: LWP::UserAgent returns hash ref with post
by choroba (Cardinal) on Apr 22, 2010 at 14:32 UTC
    What does
    print ref $r;
    output after the post call in both situations? Or, even better, use Data::Dumper and try to
    print Dumper $r;
    in both contexts.
      It's a HTTP::Response object, and I know what's in it (that's the point of the while $k $v bit).

      That's not the issue...

      One node of the hash is "_content" (a scalar/string). I'm curious as to why when this is used in a script, $r is the _content string, but here in a module function it is the entire HTTP::Response bless'd hash. Again, the code is otherwise identical.

      Whoops! No it's not...I was using:
      print "\n".$resp->content();
      In the script. Heh-heh.