start page | rating of books | rating of authors | reviews | copyrights

Perl CookbookPerl CookbookSearch this book

7.4. Making Perl Report Filenames in Error Messages

7.4.1. Problem

Your program works with files, but Perl's errors and warnings only report the last used filehandle, not the name of the file.

7.4.2. Solution

Use the filename as the filehandle:

open($path, "<", $path)
    or die "Couldn't open $path for reading : $!\n";

7.4.3. Discussion

Ordinarily, error messages say:

Argument "3\n" isn't numeric in multiply at tallyweb line 16, <LOG> chunk 17.

The filehandle LOG doesn't help much because you don't know which file the handle was connected to. By using the filename itself as indirect filehandle, Perl produces more informative errors and warnings:

Argument "3\n" isn't numeric in multiply at tallyweb
   line 16, </usr/local/data/mylog3.dat> chunk 17.

Unfortunately, this doesn't work with strict refs turned on because the variable $path doesn't really have a filehandle in it, only a string that sometimes behaves like one. The chunk number mentioned in warnings and error messages is the current value of the $. variable.

7.4.4. See Also

Recipe 7.1; the open function in perlfunc(1) and Chapter 29 of Programming Perl



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.