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