in reply to Fetch Problem uri

Regarding your second problem, the docs for File::Fetch say:

$ff->output_file The name of the output file. This is the same as $ff->file, but any qu +ery parameters are stripped off. For example: http://example.com/index.html?x=y would make the output file be index.html rather than index.html?x=y.

However, output_file() is an accessor only, so you can't change the value.

UPDATE: Better explained and shown with a patch in the reply above.

You probably would like to do something like:

my $ff = File::Fetch->new(uri => $url); my $output_name = $ff->name; $ff->file =~ /name=(.*)$/ and $output_name = $1; # $output_name is now 'v2_ITA_12-Seiter_Programm_1207_web.pdf' $ff->output_file( $output_name );

... but that doesn't work.

Update: The below errors were caused by the missing space in the filename.

That's by (poor) design, but the module seems to have other problems, as the accessor methods don't seem to do what they say:

my $ff = File::Fetch->new(uri => $url); say "scheme: " . $ff->scheme; say "host: " . $ff->host; say "path: " . $ff->path; say "file: " . $ff->file; say "output_file: " . $ff->output_file; ## outputs: Use of uninitialized value in concatenation (.) or string at ./foo.pl +line 12. scheme: host: http: path: //www.ekey.net/ file: downloads-475?download=2132cbe2-2fb1-eeff-583c-50a39b6aba +6c&name=v2_ITA_12-Seiter_Programm_1207_web.pdf output_file: downloads-475
Remember: Ne dederis in spiritu molere illegitimi!

Replies are listed 'Best First'.
Re^2: Fetch Problem uri
by Anonymous Monk on Jul 04, 2015 at 12:10 UTC

    Nice patch!

    Now I just have to figure out how to get the right file name out of the URI. Not so easy as it seems as the URI contains query parameters. I tried without success:

    my $filename = (URI->new($url)->path_segments)[-1]; my ($volume,$directories,$filename) = File::Spec->splitpath( $url );

    Strange that there is no available module that seems to cope rightly ith this URI. Or maybe is the URI to be "non standard

      (Scroll down for an answer to your latest question ...)

      Strange that there is no available module that seems to cope rightly ith this URI. Or maybe is the URI to be "non standard

      Your URI is fine (until you added a space at the start, heh). The module is maybe what is "non-standard," I am afraid.

      First the problem addressed by Perlbotics' patch; the method $ff->output_file not being a method to set the value, as it would appear to be.

      Then the ungraceful handling of a problem URI (e.g. with a leading space as in your OP):

      my $url = ' http://www.perlmonks.com/foo?bar=baz'; print "Downloading >$url<\n"; # note use of delimiters to make a stray + # leading space more visible in your deb +ug my $ff = File::Fetch->new(uri => $url); say "scheme: " . $ff->scheme; say "host: " . $ff->host; say "path: " . $ff->path; say "file: " . $ff->file; say "output_file: " . $ff->output_file;
      ## outputs: Use of uninitialized value in concatenation (.) or string at ./foo.pl +line 10. scheme: # <- error host: http: # <- error path: //www.perlmonks.com/ # <- error file: foo?bar=baz output_file: foo

      These two things alone would make me consider looking for a different solution on CPAN.

      Now I just have to figure out how to get the right file name out of the URI.

      You are on the right track with a path-parsing module. But if all your files are of the format you showed, you might want to use a regexp:

      #!/usr/bin/env perl -w use strict; my $url = 'http://www.ekey.net/downloads-475?download=2132cbe2-2fb1-ee +ff-583c-50a39b6aba6c&name=v2_ITA_12-Seiter_Programm_1207_web.pdf'; (my $output_name = $url) =~ s/^.*name=(.*)$/$1/; print "$output_name\n"; __END__
      ## outputs: v2_ITA_12-Seiter_Programm_1207_web.pdf
      Remember: Ne dederis in spiritu molere illegitimi!

        Thank you very much for your valuable feedback. I finally decided to roll back to LWP using the following"

        my $filename = (URI->new($url)->path_segments)[-1]; print "Filename is $filename \n"; $response = $browser->get($url, ':content_file' => "$filename ", ); if ($response->is_error()) { my $error= "\nCould not open $url\n", $response->status_line; }

        The problem with the file name remains (unfortunately the URIs can have any possible form so that I'm not sure your Regex will match any URI