tags:

views:

69

answers:

2

Hey.

I don't know anything about Perl but I urgently need to modify a Perl script. At some point it's downloading an about 500MB file from a server using system("lwp-download $HttpPath $Out");.

Is there any way I can find out if the downloading process went correctly, e.g. check whether downloaded file has the same size as the original one?

Thanks

A: 

In general, the system command returns* the exit status of the program that it was running. By convention a return value of zero means success, and non-zero means some sort of error.

A typical idiom is something like

my $status = system($command);
if ($status == 0) {
    # the program succeeded ...
} else {
    # the program failed ...
    warn "The program failed. Status = ", $status>>8, "\n";
}

* - sort of, see perldoc -f system for details

mobrule
@mobrule - but will "lwp-download" provide meaningful exit status to satisfy the OP's needs?
DVK
If the most important thing to know is whether the download was successful, then there's a 99% chance that the answer is yes.
mobrule
visual inspection of the lwp-download source shows a couple of places where it will fail but provide a 0 exit status :( It seems to use 0 to mean *some* data was successfully read.
ysth
This was general advice to apply to most uses of system, without addressing how it applies specifically to lwp-download, and which I never heard about until today. But the one place lwp-download can exit 0 without transferring a file is when the new file would clobber an existing file and the user does not/cannot give permission for that operation. By convention, applications should exit zero for success/non-zero for failure. Yes, from time to time you will get bitten in the ass using a program that doesn't follow convention. For the OP that doesn't know Perl, that's a lot better than nothing.
mobrule
+2  A: 

It's a dumb thing to shell out from your Perl program to run lwp-download, another Perl program. Just replace that call with the mirror API from LWP::Simple and you get decent error reporting, directly there in your program instead of messing with exit codes.

Should you need even better/fine-grained error checking, mirror from LWP::UserAgent is available, too.

daxim