File Coverage

blib/lib/Perl/Critic/Policy/Community/ConditionalDeclarations.pm
Criterion Covered Total %
statement 13 14 92.8
branch n/a
condition n/a
subroutine 5 6 83.3
pod 2 2 100.0
total 20 22 90.9


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Community::ConditionalDeclarations;
2              
3 1     1   446 use strict;
  1         2  
  1         29  
4 1     1   5 use warnings;
  1         3  
  1         27  
5              
6 1     1   5 use Perl::Critic::Utils qw(:severities :classification :ppi);
  1         2  
  1         50  
7 1     1   378 use parent 'Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations';
  1         3  
  1         5  
8              
9             our $VERSION = 'v1.0.2';
10              
11 3     3 1 17123 sub default_severity { $SEVERITY_HIGH }
12 0     0 1   sub default_themes { 'community' }
13              
14             1;
15              
16             =head1 NAME
17              
18             Perl::Critic::Policy::Community::ConditionalDeclarations - Don't declare
19             variables conditionally
20              
21             =head1 DESCRIPTION
22              
23             It is possible to add a postfix condition to a variable declaration, like
24             C<my $foo = $bar if $baz>. However, it is unclear (and undefined) if the
25             variable will be declared when the condition is not met. Instead, declare the
26             variable and then assign to it conditionally, or use the
27             L<ternary operator|perlop/"Conditional Operator"> to assign a value
28             conditionally.
29              
30             my $foo = $bar if $baz; # not ok
31             my ($foo, $bar) = @_ unless $baz; # not ok
32             our $bar = $_ for 0..10; # not ok
33             my $foo; $foo = $bar if $baz; # ok
34             my $foo = $baz ? $bar : undef; # ok
35              
36             This policy is a subclass of the core policy
37             L<Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations>, and
38             performs the same function but in the C<community> theme.
39              
40             =head1 AFFILIATION
41              
42             This policy is part of L<Perl::Critic::Community>.
43              
44             =head1 CONFIGURATION
45              
46             This policy is not configurable except for the standard options.
47              
48             =head1 AUTHOR
49              
50             Dan Book, C<dbook@cpan.org>
51              
52             =head1 COPYRIGHT AND LICENSE
53              
54             Copyright 2015, Dan Book.
55              
56             This library is free software; you may redistribute it and/or modify it under
57             the terms of the Artistic License version 2.0.
58              
59             =head1 SEE ALSO
60              
61             L<Perl::Critic>