A few pieces of advise.

sub get_object { my($agent, $url, $referer, $save_path); # args: LWP::UserAgent, + url to get, (op +tional) referer to grab that url with, (optional) path to save it to my($request, $response, $content); # http stuff $agent = shift(); $url = shift(); $referer = shift(); $save_path = shift();

Why do you need to use all these shifts? It's more concise and still clear what you're doing if you did:

my($agent, $url, $referer, $save_path) = @_; # args: # $agent - LWP::UserAgent object # $url - URL to get # $referer - referer to grab URL with (optional) # $save_path - path to save image/etc. to (optional) my($request, $response, $content); # for http stuff

Personally, I'd reccommend documenting this function with POD.

# ... $save_path ? print("Getting picture $url...") : print("Getting inf +o from $url\n");

This is better written as:

print $save_path ? "Getting picture $url..." : "Getting info from +$url\n";

(Remove redundent code like that.) update: Getting a little picky here, that's probably better written as:

print defined $save_path ? "Getting picture $url..." : "Getting in +fo from $url\n";

... because you don't want your code to fail if you try to save to the file called '0' in the current directory (though the null string, '', is an interesting case). At the same time you should change the unless ($save_path) test below. Fortunatly your $referer test is probably safe because '0' and '' are not valid referers.

You say: 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.

In most cases you can probably test for this by checking the HTTP response:

my $type = $response->headers->header("Content-Type"); if (defined $type and $type =~ /^text/) { # ... it's probably text } else { # ... probably not text }

In reply to Re: Getting pictures with LWP by wog
in thread 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.