File Coverage

lib/Spoon/CGI.pm
Criterion Covered Total %
statement 57 79 72.1
branch 19 36 52.7
condition 2 6 33.3
subroutine 13 19 68.4
pod 0 5 0.0
total 91 145 62.7


line stmt bran cond sub pod time code
1             package Spoon::CGI;
2 4     4   29082 use Spoon::Base -Base;
  4         11  
  4         43  
3 4     4   4352 use CGI -no_debug, -nosticky;
  4     4   10  
  4     4   137  
  4         23  
  4         7  
  4         178  
  4         13374  
  4         66761  
  4         43  
4             our @EXPORT = qw(cgi);
5              
6             my $all_params_by_class = {};
7              
8             const class_id => 'cgi';
9              
10             sub cgi() {
11 4     4 0 13830 my $package = caller;
12 4         7 my ($field, $is_upload, @flags);
13 4         9 for (@_) {
14 7 50       21 if ($_ eq '-upload') {
15 0         0 $is_upload = 1;
16 0         0 next;
17             }
18 7 100       67 (push @flags, $1), next if /^-(\w+)$/;
19 4   33     22 $field ||= $_;
20             }
21 4 50 33     16 die "Cannot apply flags to upload field ($field)" if $is_upload and @flags;
22 4         5 push @{$all_params_by_class->{$package}}, $field;
  4         11  
23 4     4   954 no strict 'refs';
  4         9  
  4         147  
24 4     4   24 no warnings;
  4         7  
  4         3626  
25 4         33 *{"$package\::$field"} = $is_upload
26             ? sub {
27 0     0   0 my $self = shift;
28 0         0 $self->_get_upload($field);
29             }
30             : @flags
31             ? sub {
32 6     6   3005 my $self = shift;
33 6 50       18 die "Setting CGI params not implemented" if @_;
34 6         14 my $param = $self->_get_raw($field);
35 6         11 for my $flag (@flags) {
36 6         14 my $method = "_${flag}_filter";
37 6         29 $self->$method($param);
38             }
39 6         30 return $param;
40             }
41             : sub {
42 4     4   2109 my $self = shift;
43 4 50       12 die "Setting CGI params not implemented" if @_;
44 4         19 $self->_get_raw($field);
45             }
46 4 100       28 }
    50          
47              
48 0     0 0 0 sub add_params {
49 0         0 my $class = ref($self);
50 0         0 push @{$all_params_by_class->{$class}}, @_;
  0         0  
51             }
52              
53 0     0 0 0 sub defined {
54 0         0 my $param = shift;
55 0 0       0 defined CGI::param($param) or defined CGI::url_param($param);
56             }
57              
58 0     0 0 0 sub all {
59 0         0 my $class = ref($self);
60 0         0 map { ($_, scalar $self->$_) } @{$all_params_by_class->{$class}};
  0         0  
  0         0  
61             }
62              
63 0     0 0 0 sub vars {
64 0         0 map $self->utf8_decode($_), CGI::Vars();
65             }
66              
67 10     10   14 sub _get_raw {
68 10         15 my $field = shift;
69              
70 10         13 my @values;
71 10 100       35 if (defined(my $value = $self->{$field})) {
72 5 50       16 @values = ref($value)
73             ? @$value
74             : $value;
75             }
76             else {
77 5 50       20 @values = defined CGI::param($field)
78             ? CGI::param($field)
79             : CGI::url_param($field);
80              
81             $self->utf8_decode($_)
82 5         6817 for grep defined, @values;
83              
84 5 50       22 $self->{$field} = @values > 1
85             ? \@values
86             : $values[0];
87             }
88              
89             return wantarray
90             ? @values
91 10 50       56 : defined $values[0]
    100          
92             ? $values[0]
93             : '';
94             }
95              
96 0     0   0 sub _get_upload {
97 0 0       0 my $handle = CGI::upload($_[0])
98             or return;
99 0 0       0 {handle => $handle, filename => $handle, %{CGI::uploadInfo($handle) || {}}};
  0         0  
100             }
101              
102 2     2   6 sub _utf8_filter {
103             # This is left in for backwards compatibility
104             }
105              
106 2     2   3 sub _trim_filter {
107 2         19 $_[0] =~ s/^\s*(.*?)\s*$/$1/mg;
108 2         13 $_[0] =~ s/\s+/ /g;
109             }
110              
111 2     2   3 sub _newlines_filter {
112 2 50       8 if (length $_[0]) {
113 2         11 $_[0] =~ s/\015\012/\n/g;
114 2         9 $_[0] =~ s/\015/\n/g;
115 2 50       13 $_[0] .= "\n"
116             unless $_[0] =~ /\n\z/;
117             }
118             }
119              
120             __END__