| 1 | package Perl::Critic::Policy::Community::ConditionalDeclarations; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Perl::Critic::Utils qw(:severities :classification :ppi); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use parent 'Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | our $VERSION = 'v1.0.4'; | |
| 10 | |||
| 11 | 3 | ▶ | sub default_severity { $SEVERITY_HIGH } |
|
Subroutine
default_severity: called CC=1 SCAR=0.0
Pod
documented
| |||
| 12 | 0 | ▶ | sub default_themes { 'community' } |
|
Subroutine
default_themes: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 13 | |||
| 14 | 1 | 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> | ||