1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/InputOutput/RequireCheckedSyscalls.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::InputOutput::RequireCheckedSyscalls;
15 use Perl::Critic::Utils qw{ :booleans :characters :severities :classification
16 hashify is_perl_bareword };
18 use base 'Perl::Critic::Policy';
20 our $VERSION = '1.088';
22 #-----------------------------------------------------------------------------
24 Readonly::Scalar my $DESC => q{Return value of flagged function ignored};
25 Readonly::Scalar my $EXPL => [208, 278];
27 Readonly::Array my @DEFAULT_FUNCTIONS => qw(
30 # I created this list by searching for "return" in perlfunc
31 Readonly::Array my @BUILTIN_FUNCTIONS => qw(
32 accept bind binmode chdir chmod chown close closedir connect
33 dbmclose dbmopen exec fcntl flock fork ioctl kill link listen
34 mkdir msgctl msgget msgrcv msgsnd open opendir pipe print read
35 readdir readline readlink readpipe recv rename rmdir seek seekdir
36 semctl semget semop send setpgrp setpriority setsockopt shmctl
37 shmget shmread shutdown sleep socket socketpair symlink syscall
38 sysopen sysread sysseek system syswrite tell telldir truncate
39 umask unlink utime wait waitpid
42 #-----------------------------------------------------------------------------
44 sub supported_parameters {
48 description => 'The set of functions to require checking the return value of.',
49 default_string => join( $SPACE, @DEFAULT_FUNCTIONS ),
50 behavior => 'string list',
55 sub default_severity { return $SEVERITY_LOWEST }
56 sub default_themes { return qw( core maintenance ) }
57 sub applies_to { return 'PPI::Token::Word' }
59 #-----------------------------------------------------------------------------
61 sub initialize_if_enabled {
62 my ($self, $config) = @_;
64 my @specified_functions = keys %{ $self->{_functions} };
65 my @resulting_functions;
67 foreach my $function (@specified_functions) {
68 if ( $function eq ':defaults' ) {
69 push @resulting_functions, @DEFAULT_FUNCTIONS;
71 elsif ( $function eq ':builtins' ) {
72 push @resulting_functions, @BUILTIN_FUNCTIONS;
75 push @resulting_functions, $function;
79 $self->{_functions} = { hashify(@resulting_functions) };
84 #-----------------------------------------------------------------------------
87 my ( $self, $elem, undef ) = @_;
89 return if $self->{_functions}->{':all'} ? is_perl_bareword($elem) : !$self->{_functions}->{$elem};
90 return if ! is_unchecked_call( $elem );
92 return $self->violation( $DESC . ' - ' . $elem, $EXPL, $elem );
100 #-----------------------------------------------------------------------------
108 Perl::Critic::Policy::InputOutput::RequireCheckedSyscalls - Return value of flagged function ignored.
112 This Policy is part of the core L<Perl::Critic> distribution.
117 This performs identically to InputOutput::RequireCheckedOpen/Close
118 except that this is configurable to apply to any function, whether
119 core or user-defined.
121 If your module uses L<Fatal> or C<Fatal::Exception>, then any
122 functions wrapped by those modules will not trigger this policy. For
126 open my $fh, $filename; # no violation
127 close $fh; # yes violation
131 This policy watches for a configurable list of function names. By
132 default, it applies to C<open>, C<print> and C<close>. You can
133 override this to set it to a different list of functions with the
134 C<functions> setting. To do this, put entries in a F<.perlcriticrc>
137 [InputOutput::RequireCheckedSyscalls]
138 functions = open opendir read readline readdir close closedir
140 We have defined a few shortcuts for creating this list
142 [InputOutput::RequireCheckedSyscalls]
143 functions = :defaults opendir readdir closedir
145 [InputOutput::RequireCheckedSyscalls]
146 functions = :builtins
148 [InputOutput::RequireCheckedSyscalls]
151 The C<:builtins> shortcut above represents all of the builtin
152 functions that have error conditions (about 65 of them, many of them
155 The C<:all> is the insane case: you must check the return value of
156 EVERY function call, even C<return> and C<exit>. Yes, this "feature"
157 is overkill and is wasting CPU cycles on your computer by just
158 existing. Nyah nyah. I shouldn't code after midnight.
162 Initial development of this policy was supported by a grant from the
165 This policy module is based heavily on policies written by Andrew
166 Moore <amoore@mooresystems.com>.
170 Chris Dolan <cdolan@cpan.org>
174 Copyright (c) 2007-2008 Chris Dolan. Many rights reserved.
176 This program is free software; you can redistribute it and/or modify
177 it under the same terms as Perl itself. The full text of this license
178 can be found in the LICENSE file included with this module.
182 ##############################################################################
185 # cperl-indent-level: 4
187 # indent-tabs-mode: nil
188 # c-indentation-style: bsd
190 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :