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

Hello, I use LWP to read a page and it creates a hash. When I try to dereference the hash it prints _protocolHTTP/1.1_content at the top of my output. What am I doing wrong? Thank you.

#!/usr/bin/perl use warnings; use diagnostics -verbose; #prints "_protocolHTTP/1.1_content" at the top ot the page use LWP 5.64; $nextpage = 'http://www.amazon.com/index.htm'; my $browser = LWP::UserAgent->new; my $P = $browser->get($nextpage); $P->content_type, " document!\n"; print "Content-Type: text/html\n"; print "\n\n"; #print $P; - prints HTTP::Response=HASH(0x1388fb0)} print %$P; #--- prints page but has a header "_protocolHTTP/1.1_c +ontent" close(STDOUT); exit true;

Update

Not really sure how to restore my original post. But the gist was at the top of every page I would see a hash instead of the page content. I added the %$P to try to dereference the hash but instead got "_protocolHTTP/1. 1_content" at the top of each page. Then Apple Fritter pointed me in the right direction and it now works. Thanks again guys.

# approximation of the original code $nextpage = 'http://www.Amazon.com/index.htm'; use LWP 5.64; my $browser = LWP::UserAgent->new; $P = $browser->get($nextpage); $P->content_type, " document!"; print "\n\n"; # print $P; print %$P; #attempt to dereference close(STDOUT); exit true;

Thank you Apple Fritter. You did point me in the right direction and for that I am grateful. After 4 hours of scouring the manual and trying 'solutions' that I barely understood, here is what finally worked. Posted to help the next person who is as lost as me. Lesson learned: Perl is not for the weak of heart.

use LWP::UserAgent; $nextpage = 'http://www.amazon.com/index.htm'; my $ua = new LWP::UserAgent; my $response = $ua->get($nextpage); unless ($response->is_success) { die $response->status_line; } my $P = $response->decoded_content(); if (utf8::is_utf8($content)) { binmode STDOUT,':utf8'; } else { binmode STDOUT,':raw'; } print "Content-Type: text/html\n"; print "\n\n"; print $P; close(STDOUT); exit true;

Replies are listed 'Best First'.
Re: Unintended header _protocolHTTP/1.1_content
by AppleFritter (Vicar) on Jun 23, 2014 at 22:52 UTC

    LWP::UserAgent->get() returns a HTTP::Response object. Check out the documentation for that class; you probably want the ->content() or ->decoded_content() method.

    EDIT: you're welcome! *tips hat*

    And just BTW, it's usually better to reply than to edit your original post (or only edit your original post, anyway), since only the former will cause a notification to be sent to the person you're replying to. I almost missed the update to your post -- only saw it by chance, really!