perl best practices critic

jakiś czas temu damian conway napisał "perl best practices" ("perl. najlepsze rozwiązania"). zawarł w niej szereg sugestii jak pisać.
książkę ogólnie polecam, choć nie zgadzam się ze wszystkim co napisał. ale to już inna bajka.
w oparciu o to co napisał, powstał program: perlcritic (można go zainstalować przez "install Perl::Critic" w shellu cpanowym).
program ten analizuje twój program perlowy i wypisuje błędy (błędy czytaj konstrukcje inne niż zalecane przez damiana). wraz z odnośnikami do numerów stron w książce!
warto zobaczyć jak to wygląda. przykładowo. dla jednego z moich (działających!) programów wynik perlcritica wygląda tak:

=> perlcritic archiveMails.pl
Code before strictures are enabled at line 9, column 1.  See page 429 of PBP.  (Severity: 5)
Integer with leading zeros at line 75, column 55.  See page 58 of PBP.  (Severity: 5)
Don't modify $_ in list functions at line 94, column 37.  See page 114 of PBP.  (Severity: 5)

nie najgorzej. nie? zobaczmy co się dzieje jak każę mu wyświetlać wszystkie błędy, a nie tylko krytyczne:
ojć. całości nie pokażę. za dużo, ale to mogę pokazać:

=> perlcritic -1 archiveMails.pl  | wc -l
69

to mało mówi. więc zróbmy prostą statystykę:

=> perlcritic -1 archiveMails.pl  | perl -pe 's/at line \d+, column \d+/at line X, column Y/' | sort | uniq -c | sort -nr
     14 Mixed-case variable name(s) at line X, column Y.  See page 44 of PBP.  (Severity: 1)
     10 Regular expression without "/x" flag at line X, column Y.  See page 236 of PBP.  (Severity: 3)
      9 Regular expression without "/m" flag at line X, column Y.  See page 237 of PBP.  (Severity: 2)
      6 Builtin function called with parens at line X, column Y.  See page 13 of PBP.  (Severity: 1)
      4 Postfix control "unless" used at line X, column Y.  See pages 96,97 of PBP.  (Severity: 2)
      3 Useless interpolation of literal string at line X, column Y.  See page 51 of PBP.  (Severity: 1)
      3 Subroutine does not end with "return" at line X, column Y.  See page 197 of PBP.  (Severity: 4)
      2 "unless" block used at line X, column Y.  See page 97 of PBP.  (Severity: 2)
      2 Mixed-case subroutine name at line X, column Y.  See page 44 of PBP.  (Severity: 1)
      2 File handle for "print" is not braced at line X, column Y.  See page 217 of PBP.  (Severity: 1)
      1 RCS keywords $Revision$, $Source$, $Date$ not found at line X, column Y.  See page 441 of PBP.  (Severity: 2)
      1 RCS keywords $Revision$, $HeadURL$, $Date$ not found at line X, column Y.  See page 441 of PBP.  (Severity: 2)
      1 RCS keywords $Id$ not found at line X, column Y.  See page 441 of PBP.  (Severity: 2)
      1 Postfix control "if" used at line X, column Y.  See pages 93,94 of PBP.  (Severity: 2)
      1 Package variable declared or used at line X, column Y.  See pages 73,75 of PBP.  (Severity: 3)
      1 No "VERSION" variable found at line X, column Y.  See page 404 of PBP.  (Severity: 2)
      1 Integer with leading zeros at line X, column Y.  See page 58 of PBP.  (Severity: 5)
      1 Forbid $b before $a in sort blocks at line X, column Y.  See page 152 of PBP.  (Severity: 1)
      1 Double-sigil dereference at line X, column Y.  See page 228 of PBP.  (Severity: 2)
      1 Don't modify $_ in list functions at line X, column Y.  See page 114 of PBP.  (Severity: 5)
      1 Code is not tidy at line X, column Y.  See page 33 of PBP.  (Severity: 1)
      1 Code before warnings are enabled at line X, column Y.  See page 431 of PBP.  (Severity: 4)
      1 Code before strictures are enabled at line X, column Y.  See page 429 of PBP.  (Severity: 5)
      1 Capture variable used outside conditional at line X, column Y.  See page 253 of PBP.  (Severity: 3)

łał. a przypominam – ten kod działa i robi co trzeba.
tak czy inaczej – warto spojrzeć. to co perlcritic pokazuje, to czasem całkowicie nieistotne szczegóły (jak np. mixed case variable name), ale czasem jest to coś co warto poprawić. sam po przeczytaniu best practices sporo zmieniłem w swoim stylu kodowania.

One thought on “perl best practices critic”

Comments are closed.