File Coverage

blib/lib/Perl/Critic/Policy/Freenode/Wantarray.pm
Criterion Covered Total %
statement 24 25 96.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 10 11 90.9
pod 4 5 80.0
total 42 46 91.3


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Freenode::Wantarray;
2              
3 1     1   651 use strict;
  1         2  
  1         29  
4 1     1   6 use warnings;
  1         2  
  1         42  
5              
6 1     1   7 use Perl::Critic::Utils qw(:severities :classification :ppi);
  1         2  
  1         52  
7 1     1   370 use parent 'Perl::Critic::Policy';
  1         3  
  1         5  
8              
9             our $VERSION = '0.030';
10              
11 1     1   83 use constant DESC => 'wantarray() called';
  1         3  
  1         55  
12 1     1   7 use constant EXPL => 'Context-sensitive functions lead to unexpected errors or vulnerabilities. Functions should explicitly return either a list or a scalar value.';
  1         2  
  1         176  
13              
14 2     2 0 5315 sub supported_parameters { () }
15 1     1 1 34 sub default_severity { $SEVERITY_LOW }
16 0     0 1 0 sub default_themes { 'freenode' }
17 2     2 1 9863 sub applies_to { 'PPI::Token::Word' }
18              
19             sub violates {
20 5     5 1 297 my ($self, $elem) = @_;
21 5 100 66     14 return () unless $elem eq 'wantarray' and is_function_call $elem;
22 1         382 return $self->violation(DESC, EXPL, $elem);
23             }
24              
25             1;
26              
27             =head1 NAME
28              
29             Perl::Critic::Policy::Freenode::Wantarray - Don't write context-sensitive
30             functions using wantarray
31              
32             =head1 DESCRIPTION
33              
34             Context-sensitive functions, while one way to write functions that DWIM (Do
35             What I Mean), tend to instead lead to unexpected behavior when the function is
36             accidentally used in a different context, especially if the function's behavior
37             changes significantly based on context. This also can lead to vulnerabilities
38             when a function is intended to be used as a scalar, but is used in a list, such
39             as a hash constructor or function parameter list. Instead, functions should be
40             explicitly documented to return either a scalar value or a list, so there is no
41             potential for confusion or vulnerability.
42              
43             return wantarray ? ('a','b','c') : 3; # not ok
44             return ('a','b','c'); # ok
45             return 3; # ok
46              
47             sub get_stuff {
48             return wantarray ? @things : \@things;
49             }
50             my $stuff = Stuff->new(stuff => get_stuff()); # oops! function will return a list!
51              
52             =head1 AFFILIATION
53              
54             This policy is part of L<Perl::Critic::Freenode>.
55              
56             =head1 CONFIGURATION
57              
58             This policy is not configurable except for the standard options.
59              
60             =head1 AUTHOR
61              
62             Dan Book, C<dbook@cpan.org>
63              
64             =head1 COPYRIGHT AND LICENSE
65              
66             Copyright 2015, Dan Book.
67              
68             This library is free software; you may redistribute it and/or modify it under
69             the terms of the Artistic License version 2.0.
70              
71             =head1 SEE ALSO
72              
73             L<Perl::Critic>