File Coverage

blib/lib/Perl/Critic/Policy/Freenode/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             package Perl::Critic::Policy::Freenode::Wantarray;
2              
3 1     1   565 use strict;
  1         2  
  1         23  
4 1     1   5 use warnings;
  1         2  
  1         23  
5              
6 1     1   5 use Perl::Critic::Utils qw(:severities :classification :ppi);
  1         1  
  1         57  
7 1     1   296 use parent 'Perl::Critic::Policy';
  1         3  
  1         4  
8              
9             our $VERSION = '0.032';
10              
11 1     1   66 use constant DESC => 'wantarray() called';
  1         2  
  1         45  
12 1     1   5 use constant EXPL => 'Context-sensitive functions lead to unexpected errors or vulnerabilities. Functions should explicitly return either a list or a scalar value.';
  1         1  
  1         146  
13              
14 4     4 0 11160 sub supported_parameters { () }
15 3     3 1 34 sub default_severity { $SEVERITY_LOW }
16 0     0 1 0 sub default_themes { 'freenode' }
17 4     4 1 21834 sub applies_to { 'PPI::Token::Word' }
18              
19             sub violates {
20 11     11 1 822 my ($self, $elem) = @_;
21 11 100 100     28 return () unless (($elem eq 'wantarray' or $elem->literal eq 'CORE::wantarray') and is_function_call $elem);
      66        
22 3         1038 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 CORE::wantarray ? ('a', 'b', 'c') : 3; # not ok
45             return ('a','b','c'); # ok
46             return 3; # ok
47              
48             sub get_stuff {
49             return wantarray ? @things : \@things;
50             }
51             my $stuff = Stuff->new(stuff => get_stuff()); # oops! function will return a list!
52              
53             =head1 AFFILIATION
54              
55             This policy is part of L<Perl::Critic::Freenode>.
56              
57             =head1 CONFIGURATION
58              
59             This policy is not configurable except for the standard options.
60              
61             =head1 AUTHOR
62              
63             Dan Book, C<dbook@cpan.org>
64              
65             =head1 COPYRIGHT AND LICENSE
66              
67             Copyright 2015, Dan Book.
68              
69             This library is free software; you may redistribute it and/or modify it under
70             the terms of the Artistic License version 2.0.
71              
72             =head1 SEE ALSO
73              
74             L<Perl::Critic>