Thread (11 messages) flat view 11 messages, 2 authors, 2016-06-15

Re: [PATCH 2/5] gitweb: Do not use bareword filehandles

From: Jakub Narebski <hidden>
Date: 2016-06-15 22:46:44

On Sun, 10 May 2009, Petr Baudis wrote:
On Sun, May 10, 2009 at 02:36:19AM +0200, Jakub Narebski wrote:
quoted
---
Perl::Critic::Policy::InputOutput::ProhibitBarewordFileHandles

  Write open my $fh, q{<}, $filename; instead of open FH, q{<}, $filename;.

  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
  'local'izing 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.

See also Damian Conway's book "Perl Best Practices",
chapter "10.1. Filehandles" (Don't use bareword filehandles.)


This follows similar patch for git-send-email.perl by Bill Pemberton
http://permalink.gmane.org/gmane.comp.version-control.git/117886

CC-ed Pasky, who is responsible for code in both cases...
Yeah, the book I learnt Perl from many years ago used bareword
filehandles (but it was an excellent textbook in most other aspects)
so this is a custom I have to work hard to evict. ;-)

Acked-by: Petr Baudis <redacted>
Well, on one hand this is less of an issue for standalone script like
gitweb is than for library / module.  On the other hand we use indirect
filehandles everywhere else, so it is also matter of style consistency.

"Perl Best Practices" gives as an example of bad behavior which we can
get by using bareword [global] filehandles: 1) automatic closing of
other filehandle with the same name (e.g. not only you use FILE for
bareword filehandle); 2) open might fail if there exist subroutine /
constant with the same name as filehandle e.g. EXDEV (from POSIX module).

Note however that prior to Perl 5.6 it was not that easy to avoid
using bareword filehandles. Perhaps the book in question (or author of
the textbook you mention) started with earlier Perl...


P.S. Git.pm generates surprisingly small amount of perlcritic-isms,
most of whose are either matter of style, or are false positives.
-- 
Jakub Narebski
Poland
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help