save directly to file and get filename using WWW::Mechanize

Viewed 89

I would normally save to a file using this:

$mech->save_content($mech->response->filename);

But due to some big files which cause "Out of memory" I have to use this instead:

$mech->get( $url, ":content_file"=>$tempfile );

How can I get the filename with the second method, or do I have to make one up?

I want the filename that would be returned in the response object: $mech->response->filename. I don't want to make up my own filename.

1 Answers

The :content_file option is inherited from LWP::UserAgent and behaves in the same way. You don't know the file beforehand.

You could do a HEAD request to check the filename, and then do a GET request.

Alternatively, have a look at the lwp-download utility that ships with LWP::UserAgent. It provides exactly what you need. You could use it directly, or lift the stuff you want out of it. A WWW::Mechanize object can be dropped into this code and will behave exactly the same as the LWP::UserAgent one.

Related