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