in reply to Bareword "SEEK_END" not allowed while "strict subs" in use

open( FH , "+<" , $file ) or die "Unable to open '".$file."' - $!\n";

Btw. you really should use lexical variables instead of barewords for your filehandles. Especially if you use something obvious like 'FH'.

open( my $fh , "+<" , $file ) or die "Unable to open '".$file."' - $!\n";
(and with slightly improved style)
open my $fh, '+<', $file or die "Unable to open '$file': $!\n";

Using bareword symbols to refer to file handles is particularly evil because they are global, and you have no idea if that symbol already points to some other file handle. You can mitigate some of that risk by localizing the symbol first, but that's pretty ugly. Since Perl 5.6, you can use an undefined scalar variable as a lexical reference to an anonymous filehandle. Alternatively, see the IO::Handle or IO::File or FileHandle modules for an object-oriented approach.

Replies are listed 'Best First'.
Re^2: Bareword "SEEK_END" not allowed while "strict subs" in use (FH)
by tye (Sage) on May 13, 2014 at 14:00 UTC
    Using bareword symbols to refer to file handles is particularly evil because they are global, and you have no idea if that symbol already points to some other file handle. You can mitigate some of that risk by localizing the symbol first

    It isn't particularly hard to grep for "FH" being used elsewhere in the current package. And local doesn't actually mitigate that risk much unless you only use the file handle for the duration of some short-lived subroutine (and there are no subroutines in the original code).

    Oh, and it was 5.6.1 where "open my $fh" became supported. :)

    - tye        

      *Erm* It's not about your package. It suffices if some library (which you may not even call directly) is using FH to read/write a file. Suddenly your file descriptor points to some totally different file/is closed/open for writing instead of reading or something completely different.

      I'm sure I don't have enough time to debug something like this. Do you?

        No. Test it if you don't believe me. FHs are global to the current package, just like all other non-"special" globals.

        - tye