« blib/lib/Perl/Critic/PolicyParameter.pm
↑ Summary
blib/lib/Perl/Critic/Policy/InputOutput/RequireCheckedOpen.pm »
| 1 | package Perl::Critic::Policy::InputOutput::RequireCheckedClose; | ||||||||||
| 2 | |||||||||||
| 3 | 40 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 40 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 40 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 40 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 40 | ▶ | use Perl::Critic::Utils qw{ :severities :classification }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 40 | ▶ | use parent 'Perl::Critic::Policy'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 40 | our $VERSION = '1.150'; | |||||||||
| 12 | |||||||||||
| 13 | #----------------------------------------------------------------------------- | ||||||||||
| 14 | |||||||||||
| 15 | 40 | Readonly::Scalar my $DESC => q{Return value of "close" ignored}; | |||||||||
| 16 | 40 | Readonly::Scalar my $EXPL => q{Check the return value of "close" for success}; | |||||||||
| 17 | |||||||||||
| 18 | #----------------------------------------------------------------------------- | ||||||||||
| 19 | |||||||||||
| 20 | sub supported_parameters { | ||||||||||
| 21 | return ( | ||||||||||
| 22 | { | ||||||||||
| 23 | 91 | ▶ | name => 'autodie_modules', | ||||||||
|
Subroutine
supported_parameters: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 24 | description => 'Modules which export autodie.', | ||||||||||
| 25 | default_string => 'autodie', | ||||||||||
| 26 | behavior => 'string list', | ||||||||||
| 27 | }, | ||||||||||
| 28 | ); | ||||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 75 | ▶ | sub default_severity { return $SEVERITY_LOW } | ||||||||
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 32 | 74 | ▶ | sub default_themes { return qw( core maintenance certrule ) } | ||||||||
|
Subroutine
default_themes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 33 | 31 | ▶ | sub applies_to { return 'PPI::Token::Word' } | ||||||||
|
Subroutine
applies_to: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 34 | |||||||||||
| 35 | #----------------------------------------------------------------------------- | ||||||||||
| 36 | |||||||||||
| 37 | sub violates { | ||||||||||
| 38 | 332 | ▶ | my ( $self, $elem, undef ) = @_; | ||||||||
|
Subroutine
violates: called CC=3 SCAR=17.3
Pod
documented
| |||||||||||
| 39 | |||||||||||
| 40 | 332 | ▶ | return if $elem->content() ne 'close'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | ▶ | return if ! is_unchecked_call( $elem, [ keys %{ $self->{_autodie_modules} } ] ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | |||||||||||
| 43 | 0 | return $self->violation( $DESC, $EXPL, $elem ); | |||||||||
| 44 | |||||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | 40 | 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 : | ||||||||||