seaver has asked for the wisdom of the Perl Monks concerning the following question:

Dear all,

THere's probably a more convoluted way of catching errors, and using __FILE__ to warn, but I was just interested if this option exists:

My perl script runs through a huge number of files, all named in very similar ways. They only really differ in the paths they exist in.

My problem is that Im reducing any output, so I do NOT want to use any print statements that say 'current working path is blah blah', so my log file is truly an error file.

However, one common error occurs because of wrong naming conventions within particular files, and I need to accomodate for these naming conventions, but it takes me a long time to work out which file it is, and this is especially because my error says something like:

Can't call method "blahblah" on an undefined value at /path/to/Module. +pm line 505, <FILEDATA> line 11.

So my question is, can I get Perl to output the actual filepath, instead of the name of the filehandle?

Cheers
Sam

Replies are listed 'Best First'.
Re: Forcing perl to print filenames instead
by tinita (Parson) on Mar 30, 2004 at 16:59 UTC
    I don't know of a way to get back the filename from a filehandle, and i think it's because a filehandle can be also a pipe or something else - not just a file-handle.
    but the error you're getting is telling me that you probably should test if your object exists before you're calling the method "blahblah" on it (i can just guess as you haven't shown any code). then if the object does not exist you can add the appropriate exception handling.
    for knowing which file you are processing i would probably just store the current filename in a variable.
    if that's not what you meant please elaborate and show us some (few) lines of code.
Re: Forcing perl to print filenames instead
by ambrus (Abbot) on Mar 30, 2004 at 18:45 UTC

    I think you should store the filename in a global variable, and override @SIG{qw{__DIE__ __WARN__}} so that it adds the filename to the error message.

      Ambrus

      thanks:

      This is definitely what I'm looking for, a simple override.

      Cheers
      Sam