3

It seems like FILEHANDLER is more commonly used as a handler naming convention than $filehandler. But it can give a bareword error if one forgets to use *FILEHANDLER in some constructions.

What are advantages and disadvantages of HANDLERS and $handlers?

Which naming convention is a better practise, and why?

12

It is better to use $filehandler because your filehandles will be lexically scoped. It means you won't accidentally sabotage or clash with another filehandle. You'll get a warning if you try to declare another variable with the same name. This format was introduced in Perl 5.6 so it is a newer format.

Bareword filehandles are global names. You could overwrite an existing filehandle with the same name without knowing it. It could cause some confusion if you have a constant or subroutine with the same name. And you probably won't get any warnings if you do have any of these clashes.

btw, this is one of the items listed in Perl Best Practices by Damian Conway. It's also better to use the 3-arg form of open: open my $fh, '<', $file.

0

I assume you're talking about using a FileHandle object versus a namespace/bareword or glob?

  1. Using FileHandle gives you an object. If your surrounding code is OO, it makes sense. It also makes it more readable as to when it is in scope, and how it is used. See IO::Handle

  2. Perldoc http://perldoc.perl.org/perlop.html#I%2fO-Operators

  3. I agree with stevenl, <>

  4. If you're using -w or use strict; there are issues with BAREWORDS. Safer to have a reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.