This isn't so much a problem, as a request for comments. Comments on this code, to be precise. It gets pictures or webpages, and saves pictures to $save_path or returns the body of webpages. Yeah, and it's strict and -w compatible, although it might not be -w compatible under all circumstances.
sub get_object { my($agent, $url, $referer, $save_path); # args: LWP::UserAgent, + url to get, (optional) referer to grab that url with, (optional) pat +h to save it to my($request, $response, $content); # http stuff $agent = shift(); $url = shift(); $referer = shift(); $save_path = shift(); $request = HTTP::Request->new('GET', $url); $request->referer($referer) if ($referer); # if referer param i +s specified set it as the referer header $save_path ? print("Getting picture $url...") : print("Getting inf +o from $url\n"); $response = $agent->request($request); $content = $response->content(); return($content) unless ($save_path); # if we're not saving it, + return the html # now save it if (length($content) >= 5120) # check for a dud picture { open(OUT, ">$save_path") or die("Couldn\'t open picturefile to + save: $!"); binmode(OUT); # if we're saving it it must be a picture +, therefore binary print(OUT $content); close(OUT); return(1); } else { print("Picture was a dud."); return(0); } }
It assumes that valid pictures aren't going to be under 5KB, reckless perhaps, but it suffices for the job. I just know you're gonna cuss that part to pieces, so...fire away! I was also thinking maybe I should check for binary and text data in $content, but I'm not quite sure how to do that without first writing to file and using filetest. Okay, flame away!

In reply to Getting pictures with LWP by Amoe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.