File Coverage

blib/lib/Perl/Critic/Policy/Community/Wantarray.pm
Criterion Covered Total %
statement 24 25 96.0
branch 2 2 100.0
condition 5 6 83.3
subroutine 10 11 90.9
pod 4 5 80.0
total 45 49 91.8


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