File Coverage

blib/lib/Perl/Critic/Policy/InputOutput/RequireCheckedClose.pm
Criterion Covered Total %
statement 23 26 88.4
branch 1 4 25.0
condition n/a
subroutine 11 11 100.0
pod 4 5 80.0
total 39 46 84.7


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::InputOutput::RequireCheckedClose;
2              
3 40     40   26243 use 5.010001;
  40         179  
4 40     40   248 use strict;
  40         103  
  40         821  
5 40     40   212 use warnings;
  40         121  
  40         970  
6 40     40   221 use Readonly;
  40         90  
  40         2050  
7              
8 40     40   315 use Perl::Critic::Utils qw{ :severities :classification };
  40         121  
  40         2083  
9 40     40   14211 use parent 'Perl::Critic::Policy';
  40         100  
  40         270  
10              
11             our $VERSION = '1.150';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $DESC => q{Return value of "close" ignored};
16             Readonly::Scalar my $EXPL => q{Check the return value of "close" for success};
17              
18             #-----------------------------------------------------------------------------
19              
20             sub supported_parameters {
21             return (
22             {
23 90     90 0 1898 name => 'autodie_modules',
24             description => 'Modules which export autodie.',
25             default_string => 'autodie',
26             behavior => 'string list',
27             },
28             );
29             }
30              
31 74     74 1 337 sub default_severity { return $SEVERITY_LOW }
32 74     74 1 295 sub default_themes { return qw( core maintenance certrule ) }
33 30     30 1 88 sub applies_to { return 'PPI::Token::Word' }
34              
35             #-----------------------------------------------------------------------------
36              
37             sub violates {
38 329     329 1 504 my ( $self, $elem, undef ) = @_;
39              
40 329 50       529 return if $elem->content() ne 'close';
41 0 0         return if ! is_unchecked_call( $elem, [ keys %{ $self->{_autodie_modules} } ] );
  0            
42              
43 0           return $self->violation( $DESC, $EXPL, $elem );
44              
45             }
46              
47              
48             1;
49              
50             __END__
51              
52             #-----------------------------------------------------------------------------
53              
54             =pod
55              
56             =for stopwords autodie
57              
58             =head1 NAME
59              
60             Perl::Critic::Policy::InputOutput::RequireCheckedClose - Write C<< my $error = close $fh; >> instead of C<< close $fh; >>.
61              
62             =head1 AFFILIATION
63              
64             This Policy is part of the core L<Perl::Critic|Perl::Critic>
65             distribution.
66              
67              
68             =head1 DESCRIPTION
69              
70             The perl builtin I/O function C<close> returns a false value on
71             failure. That value should be checked to ensure that the close was
72             successful.
73              
74              
75             my $error = close $filehandle; # ok
76             close $filehandle or die "unable to close: $!"; # ok
77             close $filehandle; # not ok
78              
79             use autodie qw< :io >;
80             close $filehandle; # ok
81              
82             You can use L<autodie|autodie>, L<Fatal|Fatal>, or
83             L<Fatal::Exception|Fatal::Exception> to get around
84             this. Currently, L<autodie|autodie> is not properly treated as a pragma; its
85             lexical effects aren't taken into account.
86              
87              
88             =head1 CONFIGURATION
89              
90             If you create a module that exports C<autodie> you can tell this policy about
91             it with the C<autodie_modules> setting:
92              
93             [InputOutput::RequireCheckedSyscalls]
94             autodie_modules = My::Thing
95              
96              
97             =head1 AUTHOR
98              
99             Andrew Moore <amoore@mooresystems.com>
100              
101             =head1 ACKNOWLEDGMENTS
102              
103             This policy module is based heavily on policies written by Jeffrey
104             Ryan Thalhammer <jeff@imaginative-software.com>.
105              
106             =head1 COPYRIGHT
107              
108             Copyright (c) 2007-2011 Andrew Moore. All rights reserved.
109              
110             This program is free software; you can redistribute it and/or modify
111             it under the same terms as Perl itself. The full text of this license
112             can be found in the LICENSE file included with this module.
113              
114             =cut
115              
116             ##############################################################################
117             # Local Variables:
118             # mode: cperl
119             # cperl-indent-level: 4
120             # fill-column: 78
121             # indent-tabs-mode: nil
122             # c-indentation-style: bsd
123             # End:
124             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :