5

I am running a script that pulls URLs from a database and performs a file::fetch(). From my understanding, file::fetch() only works when there is really a file in the URL to be downloaded.

However, in my database there is the occasional URL where there is no file to be fetched. So, is there any module I can use to check that there is a file in the URL I am attempting to perform a file::fetch()?

There is a line in my script

my $uri_handle = File::Fetch->new(uri => $url);

where the $url may be something like http://asiaone.com with no file to actually be fetched.

And I get this error which I am desperately trying to avoid because there are other URLs that mostly contain files to fetch.

Use of uninitialized value $path in pattern match (m//) at C:/Perl/lib/File/Spec/Unix.pm line 267.
Use of uninitialized value in string eq at C:/Perl/lib/File/Fetch.pm line 395.
Hostname required when fetching from 'http' at C:\test\multihashtest2.pl line 100 thread 2

How can I format my regular expression to check for files or is there a module I can use to facilitate this?

A legitimate URL would be something like below

http://the.earth.li/~sgtatham/putty/latest/x86/puttytel.exe
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marcus Lim
  • 567
  • 2
  • 5
  • 14

1 Answers1

3

From the manual of File::Fetch there is this flag, $File::Fetch::WARN, which you can set to false in order to silence the errors:

This variable controls whether errors encountered internally by File::Fetch should be carp'd or not.

Set to false to silence warnings. Inspect the output of the error() method manually to see what went wrong.

Defaults to true.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72
  • 1
    How do i apply that option to false? – Marcus Lim Sep 06 '12 at 09:39
  • 1
    try with `$File::Fetch::WARN = 0;` – Tudor Constantin Sep 06 '12 at 11:04
  • The same error still appears after i apply that line of code. It only removes the `Hostname required when fetching from 'http' at C:\test\multihashtest2.pl line 100 thread 2 ` error from the output. The other two errors stating uninitialized values still remain. – Marcus Lim Sep 07 '12 at 02:23
  • 1
    try adding an `eval{}` around the fetch like `my $uri_handle; eval { $uri_handle = File::Fetch->new(uri => $url); }`. This will silence all the errors :) – Tudor Constantin Sep 07 '12 at 09:50