Spoon/CGI.pm

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