in reply to Rename/mkdir with File::Fetch

my $ff = File::Fetch->new(uri=>$ref->{url}); $ff->file("document$i.pdf"); my $where = $ff->fetch ...

Replies are listed 'Best First'.
Re^2: Rename/mkdir with File::Fetch
by afoken (Chancellor) on Aug 11, 2022 at 21:22 UTC

    $ff->file() is an unfortunate name, it is documented as "The name of the remote file". At least for HTTP, a document available via a URI might come from a database, or it might just be generated on the fly, so there might be no file at all.

    Plus, calling $ff->file(...) before calling $ff->fetch(...) will change the source URI for at least some URI schemes and fetch methods, which is NOT the desired behaviour.

    But: The documentation for $ff->file() has a second sentence, and that's the real hint:

    For the local file name, the result of $ff->output_file will be used.

    It might look like you could simply call $ff->output_file("document$i.pdf"), but a quick look into the source code shows that more work is needed:

    sub output_file { my $self = shift; my $file = $self->file; $file =~ s/\?.*$//g; $file ||= $self->file_default; return $file; }

    So, you need to replace output_file, either by monkeypatching or by inheriting from File::Fetch and replacing that method with one that returns the desired file name.

    Or, if all you need is to fetch from HTTP or HTTPS, follow hippo's++ advice and use LWP::Simple.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      ?? Did you try it?? I'm on mobile