line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
=head1 NAME |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
CGI::Enurl.pm - module for URL-encoding strings and hashes |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
version 1.07 |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 SYNOPSIS |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use CGI::Enurl; |
11
|
|
|
|
|
|
|
%hash = (name=>'Jenda Krynicky',address=>'Nerudova 1016'); |
12
|
|
|
|
|
|
|
print "Location: http://$ENV{SERVER_NAME}/cgi-bin/do.pl?",enurl \%hash,"\n\n"; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 DESCRIPTION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
This is a little module made for CGI scripting. It encodes the parameters |
17
|
|
|
|
|
|
|
to be passed to a CGI. It does nothing more, so it's much smaller and loads |
18
|
|
|
|
|
|
|
more quickly. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 Functions |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=over 2 |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=item enurl STRING |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=item enurl ARRAY |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=item enurl HASH |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
Encodes the parameter. If the parameter is a single string |
31
|
|
|
|
|
|
|
it encodes it and returns the encoded form. |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
If it is an array or a reference to an array it encodes all |
34
|
|
|
|
|
|
|
items and returns them joined by '&'. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
If it is a hash it encodes the values and return a querystring in form |
37
|
|
|
|
|
|
|
"key2=encoded_value1&key2=encoded_value2&...". |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
!!! Please note that a hash in a list context returns a list of all |
40
|
|
|
|
|
|
|
keys and values. This means that if you call enurl(%hash) you will NOT |
41
|
|
|
|
|
|
|
get what you may thing you should. You HAVE to use enurl(\%hash) !!! |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=item enURL STRING |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Encodes the parameter, this version doesn't encode '=' and '&' characters, |
46
|
|
|
|
|
|
|
so you should make sure they are not present in the data. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Notice the difference : |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
enurl 'a&b=f o o' => 'a%26b%3Df+o+o' |
51
|
|
|
|
|
|
|
enURL 'a&b=f o o' => 'a&b=f+o+o' |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item $CGI::Enurl::ParamSeparator |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
You may specify another character to be used as the parameter separator. |
56
|
|
|
|
|
|
|
Simply set this variable to the character (or string) you want to use. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
The default value is '&' |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item $CGI::Enurl::KeepUnencoded |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
This variable contains the characters that should stay unencoded. |
63
|
|
|
|
|
|
|
Please keep in mind that the string will be interpolated into a regexp |
64
|
|
|
|
|
|
|
in a [^...] group! |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Any change of this variable will be ignored after the first call to |
67
|
|
|
|
|
|
|
enurl or enURL. (I'm using /o switch in the regexp.) So if you want to |
68
|
|
|
|
|
|
|
change the variable you should do it as soon as posible. You may do that |
69
|
|
|
|
|
|
|
even before you "use" the module! |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
The default value is 'a-zA-Z 0-9_\\-@.=' |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=back |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head2 EXAMPLE: |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
use CGI::Enurl; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
print "Location: http://www.somewhere.com/Scripts/search.pl?", |
80
|
|
|
|
|
|
|
enurl('something strange'),"\n\n"; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
or |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
use CGI::Enurl; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
print "Location: http://www.somewhere.com/Scripts/search.pl?", |
87
|
|
|
|
|
|
|
enurl('something strange','and other',666),"\n\n"; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
or |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
use CGI::Enurl; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
print "Location: http://www.somewhere.com/Scripts/myscript.pl?", |
94
|
|
|
|
|
|
|
enurl({fname => 'Jan',lname => 'Krynický',tel => '+420-2-9618 1234'},1),"\n\n"; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
or |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
use CGI::Enurl; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
print "Location: http://www.somewhere.com/Scripts/myscript.pl?", |
101
|
|
|
|
|
|
|
enURL('fname=Jan&lname=Krynický&tel=+420-2-9618 1234&1',"\n\n"; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
or using the tricks of Interpolation.pm - http://www.plover.com/~mjd/perl/Interpolation/manual.html |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
use CGI::Enurl; |
107
|
|
|
|
|
|
|
use Interpolation URL => \&enurl; |
108
|
|
|
|
|
|
|
print "name=$URL{'Jann Linder, jr'}&address=$URL{'129 kjhlkjd st'}"; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
or even |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
use CGI::Enurl; |
113
|
|
|
|
|
|
|
use Interpolation enurl => sub {my %hash=split /$;/o,$_[0];enurl \%hash}; |
114
|
|
|
|
|
|
|
# use other name instead of enurl if you like. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
print "script.pl?$enurl{name=>'Jenda Krynicky',address=>'Nerudova 1016'}\n"; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
%hash = (name=>'Jenda Krynicky',address=>'Nerudova 1016'); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub var { |
121
|
|
|
|
|
|
|
if (ref $_[0] eq 'HASH') { |
122
|
|
|
|
|
|
|
join $;, %{shift()}, @_; |
123
|
|
|
|
|
|
|
} else { |
124
|
|
|
|
|
|
|
join $;, @_; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
print "script.pl?$enurl{var %hash}\n"; |
129
|
|
|
|
|
|
|
# the "var" is necessary ! |
130
|
|
|
|
|
|
|
# without it you will get : "Odd number of elements in hash list at ... line 2." |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
print "script.pl?$enurl{var %hash,age=>22}\n"; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# you may omit the "var" only if you enter the hash as a constant directly |
135
|
|
|
|
|
|
|
# into $enurl{...}. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
If you want to be cheeky you may use '$?{}' as the interpolator: |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
use CGI::Enurl; |
140
|
|
|
|
|
|
|
use Interpolation '?' => sub {my %hash=split /$;/o,$_[0]; '?' . enurl \%hash}; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
print "cript.pl$?{a=>5,b=>7,n=>'Jenda Krynicky'}\n"; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
or |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
use CGI::Enurl; |
147
|
|
|
|
|
|
|
use Interpolation '?' => sub {'?' . enURL $_[0]}; |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
print "cript.pl$?{'a=5&b=7&n=Jenda Krynicky'}\n"; |
150
|
|
|
|
|
|
|
# # or |
151
|
|
|
|
|
|
|
# print qq{cript.pl$?{"a=5&b=7&n=$name"}\n}; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Please read the docs for enurl versus enURL so that you understand the difference! |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=cut |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
#!/big/bin/perl |
158
|
|
|
|
|
|
|
package CGI::Enurl; |
159
|
|
|
|
|
|
|
$VERSION='1.07'; |
160
|
|
|
|
|
|
|
require Exporter; |
161
|
|
|
|
|
|
|
@ISA = (Exporter); |
162
|
|
|
|
|
|
|
@EXPORT = qw(&enurl &enURL); |
163
|
|
|
|
|
|
|
@EXPORT_OK = qw(&enurl &enURL &enurl_str); |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
$ParamSeparator = '&' unless $ParamSeparator; |
166
|
|
|
|
|
|
|
$KeepUnencoded = 'a-zA-Z 0-9_\\-@.=' unless $KeepUnencoded; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
sub enurl { |
169
|
7
|
|
|
7
|
1
|
408
|
my @data; |
170
|
|
|
|
|
|
|
my $item; |
171
|
7
|
|
|
|
|
14
|
foreach $item (@_) { |
172
|
8
|
100
|
|
|
|
27
|
if (ref $item eq 'HASH') { |
|
|
100
|
|
|
|
|
|
173
|
4
|
|
|
|
|
5
|
my $key; |
174
|
4
|
|
|
|
|
13
|
foreach $key (keys %$item) { |
175
|
7
|
100
|
|
|
|
26
|
if ($key =~ /^\d+$/) { |
176
|
3
|
50
|
|
|
|
9
|
if (ref $item->{$key} eq 'ARRAY') { |
177
|
0
|
|
|
|
|
0
|
foreach (@{$item->{$key}}) { |
|
0
|
|
|
|
|
0
|
|
178
|
0
|
|
|
|
|
0
|
push @data,enurl_str($_); |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
} else { |
181
|
3
|
|
|
|
|
9
|
push @data,enurl_str($item->{$key}); |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
} else { |
184
|
4
|
50
|
|
|
|
12
|
if (ref $item->{$key} eq 'ARRAY') { |
185
|
0
|
|
|
|
|
0
|
foreach (@{$item->{$key}}) { |
|
0
|
|
|
|
|
0
|
|
186
|
0
|
|
|
|
|
0
|
push @data,(enurl_str($key).'='.enurl_str($_)); |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
} else { |
189
|
4
|
|
|
|
|
8
|
push @data,(enurl_str($key).'='.enurl_str($item->{$key})); |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
} elsif (ref $item eq 'ARRAY') { |
194
|
1
|
|
|
|
|
2
|
my $x; |
195
|
1
|
|
|
|
|
3
|
foreach $x (@$item) { |
196
|
2
|
|
|
|
|
4
|
push @data,enurl_str($x); |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
} else { |
199
|
3
|
|
|
|
|
6
|
push @data,enurl_str($item); |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
} |
202
|
7
|
|
|
|
|
29
|
return (join $ParamSeparator, @data); |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
#sub enurl_str ($); |
206
|
|
|
|
|
|
|
sub enurl_str { |
207
|
16
|
|
|
16
|
0
|
26
|
my($toencode) = @_; |
208
|
16
|
|
|
|
|
63
|
$toencode=~s/([^$KeepUnencoded])/sprintf("%%%02X",ord($1))/ego; |
|
0
|
|
|
|
|
0
|
|
209
|
16
|
|
|
|
|
32
|
$toencode=~s/ /+/gm; |
210
|
16
|
|
|
|
|
61
|
return $toencode; |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
#sub enURL ($); |
214
|
|
|
|
|
|
|
sub enURL { |
215
|
0
|
|
|
0
|
1
|
|
my($toencode) = @_; |
216
|
0
|
|
|
|
|
|
$toencode=~s/([^$ParamSeparator$KeepUnencoded])/sprintf("%%%02X",ord($1))/ego; |
|
0
|
|
|
|
|
|
|
217
|
0
|
|
|
|
|
|
$toencode=~s/ /+/gm; |
218
|
0
|
|
|
|
|
|
return $toencode; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
1; |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head2 DISCLAIMER |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
The enurl_str function is taken from CGI.pm. (It's named 'escape' there.) Thanks. |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head2 AUTHOR |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
Jan Krynicky |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head2 COPYRIGHT |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Copyright (c) 1997-2001 Jan Krynicky . All rights reserved. |
234
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
235
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=cut |
238
|
|
|
|
|
|
|
|