File Coverage

blib/lib/PLP/Fields.pm
Criterion Covered Total %
statement 18 33 54.5
branch 3 12 25.0
condition 2 18 11.1
subroutine 3 5 60.0
pod 0 1 0.0
total 26 69 37.6


line stmt bran cond sub pod time code
1             package PLP::Fields;
2              
3 1     1   6 use strict;
  1         2  
  1         40  
4 1     1   5 use warnings;
  1         2  
  1         705  
5              
6             our $VERSION = '1.00';
7              
8             # Has only one function: doit(), which ties the hashes %get, %post, %fields
9             # and %header in PLP::Script. Also generates %cookie immediately.
10             sub doit {
11              
12             # %get
13            
14 18     18 0 30 my $get = \%PLP::Script::get;
15 18 50 33     104 if (defined $ENV{QUERY_STRING} and length $ENV{QUERY_STRING}){
16 18         87 for (split /[&;]/, $ENV{QUERY_STRING}) {
17 36         97 my @keyval = split /=/, $_, 2;
18 36         113 PLP::Functions::DecodeURI(@keyval);
19 36 50       115 $get->{$keyval[0]} = $keyval[1] unless $keyval[0] =~ /^\@/;
20 36         36 push @{ $get->{ '@' . $keyval[0] } }, $keyval[1];
  36         132  
21             }
22             }
23              
24             # %post
25              
26             tie %PLP::Script::post, 'PLP::Tie::Delay', 'PLP::Script::post', sub {
27 0     0   0 my %post;
28 0 0 0     0 return \%post unless $ENV{CONTENT_TYPE} and $ENV{CONTENT_LENGTH} and
      0        
29             $ENV{CONTENT_TYPE} =~ m!^(?:application/x-www-form-urlencoded|$)!;
30            
31 0         0 my $post = $PLP::read->($ENV{CONTENT_LENGTH});
32 0 0 0     0 return \%post unless defined $post and length $post;
33            
34 0         0 for (split /&/, $post) {
35 0         0 my @keyval = split /=/, $_, 2;
36 0         0 PLP::Functions::DecodeURI(@keyval);
37 0 0       0 $post{$keyval[0]} = $keyval[1] unless $keyval[0] =~ /^\@/;
38 0         0 push @{ $post{ '@' . $keyval[0] } }, $keyval[1];
  0         0  
39             }
40            
41 0         0 return \%post;
42 18         158 };
43              
44             # %fields
45              
46             tie %PLP::Script::fields, 'PLP::Tie::Delay', 'PLP::Script::fields', sub {
47 0     0   0 return { %PLP::Script::get, %PLP::Script::post };
48 18         99 };
49              
50             # %header
51              
52 18         100 tie %PLP::Script::header, 'PLP::Tie::Headers';
53              
54             # %cookie
55              
56 18 50 33     80 if (defined $ENV{HTTP_COOKIE} and length $ENV{HTTP_COOKIE}) {
57 0           for (split /; ?/, $ENV{HTTP_COOKIE}) {
58 0           my @keyval = split /=/, $_, 2;
59 0   0       $PLP::Script::cookie{$keyval[0]} ||= $keyval[1];
60             }
61             }
62             }
63              
64             1;
65              
66             =head1 NAME
67              
68             PLP::Fields - Special hashes for PLP
69              
70             =head1 DESCRIPTION
71              
72             For your convenience, PLP uses hashes to put things in. Some of these are tied
73             hashes, so they contain a bit magic. For example, building the hash can be
74             delayed until you actually use the hash.
75              
76             =over 10
77              
78             =item C<%get> and C<%post>
79              
80             These are built from the C (or C
81             strings in query string and post content. C<%post> is not built if the content
82             type is not C. In post content, the
83             semi-colon is not a valid separator.
84              
85             %post isn't built until it is used, to speed up your script if you
86             don't use it. Because POST content can only be read once, you can C
87             and just never access C<%post> to avoid its building.
88              
89             With a query string of C, C<$get{key}> will
90             contain only C. You can access both elements by using the array
91             reference C<$get{'@key'}>, which will contain C<[ 'firstvalue', 'secondvalue'
92             ]>.
93              
94             =item C<%fields>
95              
96             This hash combines %get and %post, and triggers creation of %post. POST gets
97             precedence over GET (note: not even the C<@>-keys contain both values).
98              
99             This hash is built on first use, just like %post.
100              
101             =item C<%cookie>, C<%cookies>
102              
103             This is built immediately, because cookies are usually short in length. Cookies
104             are B automatically url-decoded.
105              
106             =item C<%header>, C<%headers>
107              
108             In this hash, you can set headers. Underscores are converted to normal minus
109             signs, so you can leave out quotes. The hash is case insensitive: the case used
110             when sending the headers is the one you used first. The following are equal:
111              
112             $header{CONTENT_TYPE}
113             $header{'Content-Type'}
114             $header{Content_Type}
115             $headers{CONTENT_type}
116              
117             If a value contains newlines, the header is repeated for each line:
118              
119             $header{Allow} = "HEAD\nGET"; # equivalent to HEAD,GET
120              
121             =back
122              
123             =head1 AUTHOR
124              
125             Juerd Waalboer
126              
127             Current maintainer: Mischa POSLAWSKY
128              
129             =cut
130