Login | Register
My pages Projects Community openCollabNet

Discussions > users > How to use Perl::Critic ? – the efficient way.

perlcritic
Discussion topic

Back to topic list

How to use Perl::Critic ? – the efficient way.

Author mduc
Full name m. Duc
Date 2010-10-16 05:11:17 PDT
Message Hi,

Lately I'm trying to write the cleanest code as possible.

So of course, I'm using Perl::Critic, as well as perltidy.

I'm also frequently referring to the following urls:
http://www.modernper​lbooks.com/mt/2010/0​2/a-perl-programming​-maintenance-checkli​st.html
http://semver.org/
http://tomdoc.org/
http://tom.preston-w​erner.com/2010/08/23​/readme-driven-devel​opment.html

(Some of the need some adaptation to be used with Perl scripts).


Anyway, I tried to write a simple "hello world" script without warnings
from Perl::Critic.

Well, I turned out it wasn't as easy as I expected.


That led to some questions.

Here is the original script:
--------------------​-------------8<--​--------------------​-----------
#! /usr/bin/perl

use strict;
use warnings;

print "hello world\n";
--------------------​------------->8--​--------------------​-----------

And here is the Perl::Critic result:
--------------------​-------------8<--​--------------------​-----------
RCS keywords $Id$ not found at line 1, column 1.  See page 441 of PBP.
 Severity: 2
RCS keywords $Revision$, $HeadURL$, $Date$ not found at line 1, column
1.  See page 441 of PBP.  Severity: 2
RCS keywords $Revision$, $Source$, $Date$ not found at line 1, column
1.  See page 441 of PBP.  Severity: 2
No "$VERSION" variable found at line 1, column 1.  See page 404 of
PBP.  Severity: 2
Return value of flagged function ignored - print at line 7, column 1.
See pages 208,278 of PBP.  Severity: 1
--------------------​------------->8--​--------------------​-----------

Ok so I need:
1/ Some RCS keywords (which ones? It's pretty unclear to me)
2/ A $VERSION variable
3/ To test the return value of print.

On 1/, according to the first line, I need an $Id$, and according to
the 2 following lines, I think I need ($Revision$ and $Date$) and
$HeadURL$ or $Source$.

Ok so let's try this. For the date, no problem, let's try today:
# $Date: 2010-09-10 $

That was the easy part. Now the $Revision$. I'm not sure what to use.
The page 441 of PBP tells me that "Every code file, no matter how
small, should be kept in a source-control repository". That's fine, I'm
using git for pretty much everything I write (including code,
configuration files, LaTeX document, Curriculum Vitae and so on). Now
the question is: How do I write in my source file, the commit id of the
repository containing it? If I write the actual commit, the file would have
changed. Then I'll need to commit again and it will make another id that
I would have to write in the file and so on. So how can I handle this?

For now, I'll just make an arbitrary 3-digit version respecting SemVer
guidelines:
# $Revision: 1.0.0 $

Ok now a $HeadURL$ or $Source$. As this is only for my own use, I don't
have a public (or private) url. Let's use Source then.
# $Source: git://repo/projects/​test_pc/test.pl $


Ok so I now have $Date$, $Revision$ and $Source$, I should only have
the 1st of the 3 warning lines (the one about $Id$, not the 2 others).
Let's check.

Well no. The tree lines disappeared. I don't really get the point of
the 3 lines then. Wouldn't it be much easier to have a unique warning
"RCS keywords missing" and then details the recommended combinations
in the appropriate PBP page?



Ok now, only 2 more lines to handle. Let's back to 2/. We need a
$VERSION variable. That should be easy. I'll use the same version
number (1.0.0):
our $VERSION = '1.0.0';

It works. No more warnings. But still I'm annoyed because I had to
write this number twice. I'm afraid it might led to errors in the future.
It's so easy to forgot to replace one of the two occurrences.


I'll try the PBP page recommendation then:
our ($VERSION) = '$Revision: 1.0.0 $' =~ m{ \$Revision: \s+ (\S+) }x;

But this led to 4 new errors, only 2 after I added ms to the m// operator:
--------------------​-------------8<--​--------------------​-----------
$VERSION value must be a constant at line 10, column 6.  Computed
$VERSION may tie the code to a single repository, or cause spooky
action from a distance.  Severity: 2
String *may* require interpolation at line 10, column 18.  See page 51
of PBP.  Severity: 1
--------------------​------------->8--​--------------------​-----------

Ok so let's go back to the old way to remove the warnings. But I'm
thrilled. PBP examples doesn't seem to be PBP compliant. That's not
always easy to deal with. I try to use to make my code cleaner. So I
try to get advices from PBP, and write somewhere in my head "that's
how things should be done", and then I have to re-learn that this
violates other policies, and that, after all, that wasn't such a good idea.


Anyway. On the last warning, 3/, I'll just addd a croak after the print:
--------------------​-------------8<--​--------------------​-----------
#! /usr/bin/perl

# $Date: 2010-09-10 $
# $Revision: 1.0.0 $
# $Source: /usr/share/projects/​test_pc/test.pl $

use strict;
use warnings;
use Carp;

our $VERSION = '1.0.0';

print "hello world\n" or croak "failed\n";
--------------------​------------->8--​--------------------​-----------

But I can help but wonder: Do we really have to check print return?
Does it ever fails? Do people really test it?


That's all for now. Many questions that seek answers. But still, I'm
happy Perl::Critic exists. That helped me a lot. Thank you for doing
this.

« Previous message in topic | 1 of 9 | Next message in topic »

Messages

Show all messages in topic

How to use Perl::Critic ? – the efficient way. mduc m. Duc 2010-10-16 05:11:17 PDT
     Re: How to use Perl::Critic ? – the efficient way. turnstep Greg Sabino Mullane 2010-10-16 06:59:39 PDT
         Re: How to use Perl::Critic ? – the efficient way. clonezone Elliot Shank 2010-10-16 08:03:23 PDT
             Re: How to use Perl::Critic ? – the efficient way. turnstep Greg Sabino Mullane 2010-10-18 09:32:10 PDT
                 Re: How to use Perl::Critic ? – the efficient way. clonezone Elliot Shank 2010-10-18 17:12:54 PDT
                     Re: How to use Perl::Critic ? – the efficient way. turnstep Greg Sabino Mullane 2010-10-19 06:30:37 PDT
     Re: How to use Perl::Critic ? – the efficient way. packy Packy Anderson 2010-10-18 10:13:38 PDT
     Re: How to use Perl::Critic ? – the efficient way. thaljef Jeffrey Ryan Thalhammer 2010-10-18 10:33:33 PDT
     Re: How to use Perl::Critic ? – the efficient way. clonezone Elliot Shank 2010-10-18 17:19:55 PDT
Messages per page: