blib/lib/Perl/Critic/Policy/InputOutput/RequireCheckedOpen.pm

filter: Statementstmt 90.0% 27 / 30 Branchbran 25.0% 1 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 80.0% 4 / 5 totaltotal 86.0% 43 / 50 CC 3 SCAR 7.3
CC
3
Cov
82%
violates
5.7
BEGIN
1.0
BEGIN
1.0
CRAP 2.1
« blib/lib/Perl/Critic/Policy/InputOutput/RequireCheckedClose.pm ↑ Summary blib/lib/Perl/Critic/Policy/Subroutines/ProhibitExcessComplexity.pm »
1 package Perl::Critic::Policy::InputOutput::RequireCheckedOpen;
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 "open" ignored};
16 40 Readonly::Scalar my $EXPL => q{Check the return value of "open" 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_MEDIUM }
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 'open';
Branch: 1 branch
BranchTrueFalseTotal
if $elem->content ne "open" 332 0 332
41 0 ▶ return if ! is_unchecked_call( $elem, [ keys %{ $self->{_autodie_modules} } ] );
Branch: 1 branch
BranchTrueFalseTotal
unless is_unchecked_call($elem, [keys %{$$self{"_autodie_modules"};}]) 0 0 0
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::RequireCheckedOpen - Write C<< my $error = open $fh, $mode, $filename; >> instead of C<< open $fh, $mode, $filename; >>.
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<open> returns a false value on
71 failure. That value should always be checked to ensure that the open
72 was successful.
73  
74  
75 my $error = open( $filehandle, $mode, $filename ); # ok
76 open( $filehandle, $mode, $filename ) or die "unable to open: $!"; # ok
77 open( $filehandle, $mode, $filename ); # not ok
78  
79 use autodie;
80 open $filehandle, $mode, $filename; # 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 :
« blib/lib/Perl/Critic/Policy/InputOutput/RequireCheckedClose.pm ↑ Summary blib/lib/Perl/Critic/Policy/Subroutines/ProhibitExcessComplexity.pm »