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
}
|