tags:

views:

82

answers:

2
use warnings;
use Test::More;
use File::Find::Rule;
use Test::File::Find::Rule;

my $rule = File::Find::Rule->file->name('*.pl')->not_grep(qr/^\s*use\s+strict;/m, sub { 1 });
match_rule_no_result($rule, ".", 'use strict usage');
done_testing();

and the output was :

out put :
ok 1 - use strict usage
1..1

it is always passing the test even when my script doesn't use strict, just like this script which is located inside "." directory. the same code is available as an example at http://search.cpan.org/~fabpot/Test-File-Find-Rule-1.00/lib/Test/File/Find/Rule.pm

any clue ?

F.

+3  A: 

To check for use strict just do this:

my $rule = File::Find::Rule
    ->file->name('*.pl')
    ->not_grep( qr/^\s*use\s+strict;/ )
;

Update

I agree with Schwern and jrockway: there's a better module for enforcing use strict.

That said, here's what I was able to figure out about the particulars of your question.

The use strict example provided by Test::File::Find::Rule is misguided.

As I understand it, the grep method in File::Find::Rule will evaluate each line of a file, using each specifier provided, stopping (i.e., retaining the file in its result set) on the first true evaluation. File::Find::Rule provides an example of making sure every file begins with a shebang line. If the first line fails the regex, the next specifier (the anonymous subroutine) will always return true and the non-conforming file will be found.

$rule->grep( qr/^#!.*\bperl/, [ sub { 1 } ] );

For a use strict test, however, you don't want to restrict yourself to the first line. Also, the not_grep method makes the additional specifiers unnecessary: we will retain the file if none of the lines in the file match the regex. Hope that helps.

FM
I don't think this should be the accepted answer, but it is technically correct, so I gave it +1. But Schwern's idea is better :)
jrockway
+8  A: 

Consider using Perl::Critic instead, which can do this more reliably, is configurable, and do a whole lot more. There is even Test::Perl::Critic to enforce it.

Perl::Critic also has the advantage of being aware of things like Moose which turn on strictures.

Schwern
+1. This is the right answer. Don't write this yourself when something else already handles the corner cases that you haven't even though of yet.
jrockway