1 package PLP::Functions;
6 use Exporter qw(import);
9 our @EXPORT = qw/Entity DecodeURI EncodeURI Include include PLP_END
10 AddCookie ReadFile WriteFile AutoURL Counter exit/;
18 eval 'package PLP::Script; ' . PLP::source($PLP::file, 0, join ' ', (caller)[2,1]);
20 PLP::Functions::exit() if $@ =~ /\cS\cT\cO\cP/;
34 push @PLP::END, shift;
38 my $ref = defined wantarray ? [@_] : \@_;
46 s/\t/ /g;
50 return defined wantarray ? (wantarray ? @$ref : "@$ref") : undef;
54 my $ref = defined wantarray ? [@_] : \@_;
57 tr/+/ /; # Browsers do tr/ /+/ - I don't care about RFCs, but
58 # I do care about real-life situations.
59 s/%([0-9A-Fa-f][0-9A-Fa-f])/chr hex $1/ge;
62 return defined wantarray ? (wantarray ? @$ref : "@$ref") : undef;
66 my $ref = defined wantarray ? [@_] : \@_;
69 s{([^A-Za-z0-9\-_.!~*'()/?:@\$,])}{sprintf("%%%02x", ord $1)}ge;
72 return defined wantarray ? (wantarray ? @$ref : "@$ref") : undef;
76 if ($PLP::Script::header{'Set-Cookie'}) {
77 $PLP::Script::header{'Set-Cookie'} .= "\nSet-Cookie: $_[0]";
79 $PLP::Script::header{'Set-Cookie'} = $_[0];
85 open (my $fh, '<', $_[0]) or do {
86 PLP::error("Cannot open $_[0] for reading ($!)", 1);
95 open (my $fh, '>', $_[0]) or do {
96 PLP::error("Cannot open $_[0] for writing ($!)", 1);
100 print $fh $_[1] or do {
101 PLP::error("Cannot write to $_[0] ($!)");
105 PLP::error("Cannot close $_[0] ($!)");
114 open $fh, '+<', $_[0] or
115 open $fh, '>', $_[0] or return undef;
121 print $fh ++$counter or return undef;
122 close $fh or return undef;
127 # This sub assumes your string does not match /(["<>])\cC\1/
128 my $ref = defined wantarray ? \(my $copy = $_[0]) : \$_[0];
130 $$ref =~ s/"/"\cC"/g; # Single characters are easier to match :)
131 $$ref =~ s/>/>\cC>/g; # so we can just use a character class []
132 $$ref =~ s/</<\cC</g;
134 # Now this is a big, ugly regex! But hey - it works :)
135 $$ref =~ s{((\w+://|www\.|WWW\.)[a-zA-Z0-9\.\@:-]+[^\"\'>< \r\t\n]*)}{
138 s/// if (my $trailing) = /([\.,!\?\(\)\[\]]+$)/;
139 s/&(?!\x23?\w+;)/&/g;
141 my $href = ($scheme =~ /www\./i ? "http://$_" : $_);
142 qq{<a href="$href" target="_blank">$_</a>$trailing};
145 $$ref =~ s/"\cC"/"/g;
146 $$ref =~ s/>\cC>/>/g;
147 $$ref =~ s/<\cC</</g;
149 if ($@){ return defined wantarray ? @_ : undef } # return original on error
150 return defined wantarray ? $$ref : undef;
157 PLP::Functions - Functions that are available in PLP documents
161 The functions are exported into the PLP::Script package that is used by PLP documents. Although uppercased letters are unusual in Perl, they were chosen to stand out.
163 Most of these functions are context-hybird. Before using them, one should know about contexts in Perl. The three major contexts are: B<void>, B<scalar> and B<list> context. You'll find more about context in L<perlfunc>.
165 Some context examples:
167 print foo(); # foo is in list context (print LIST)
168 foo(); # foo is in void context
169 $bar = foo(); # foo is in scalar context
170 @bar = foo(); # foo is in list context
171 length foo(); # foo is in scalar context (length EXPR)
177 =item Include FILENAME
179 Executes another PLP file, that will be parsed (i.e. code must be in C<< <: :> >>). As with Perl's C<do>, the file is evaluated in its own lexical file scope, so lexical variables (C<my> variables) are not shared. PLP's C<< <(filename)> >> includes at compile-time, is faster and is doesn't create a lexical scope (it shares lexical variables).
181 Include can be used recursively, and there is no depth limit:
183 <!-- This is crash.plp -->
186 # This example will loop forever,
187 # and dies with an out of memory error.
188 # Do not try this at home.
191 =item include FILENAME
193 An alias for C<Include>.
197 Adds a piece of code that is executed when at the end of the PLP document. This is useful when creating a template file:
199 <html><body> <!-- this is template.plp -->
204 <(template.plp)> <!-- this is index.plp -->
207 You should use this function instead of Perl's built-in C<END> blocks, because those do not work properly with mod_perl.
211 Replaces HTML syntax characters by HTML entities, so they can be displayed literally. You should always use this when displaying user input (or database output), to avoid cross-site-scripting vurnerabilities.
213 In void context, B<changes> the values of the given variables. In other contexts, returns the changed versions.
215 <: print Entity($user_input); :>
217 Be warned that this function also HTMLizes consecutive whitespace and newlines (using and <br> respectively).
218 For simple escaping, use L<XML::Quote|XML::Quote>.
219 To escape high-bit characters as well, use L<HTML::Entities|HTML::Entities>.
223 Encodes URI strings according to RFC 3986. All disallowed characters are replaced by their %-encoded values.
225 In void context, B<changes> the values of the given variables. In other contexts, returns the changed versions.
227 <a href="/foo.plp?name=<:= EncodeURI($name) :>">Link</a>
229 Note that the following reserved characters are I<not> percent-encoded, even though they may have a special meaning in URIs:
233 This should be safe for escaping query values (as in the example above),
234 but it may be a better idea to use L<URI::Escape|URI::Escape> instead.
238 Decodes %-encoded strings. Unlike L<URI::Escape|URI::Escape>,
239 it also translates + characters to spaces (as browsers use those).
241 In void context, B<changes> the values of the given variables. In other contexts, returns the changed versions.
243 =item ReadFile FILENAME
245 Returns the contents of FILENAME in one large string. Returns undef on failure.
247 =item WriteFile FILENAME, STRING
249 Writes STRING to FILENAME (overwrites FILENAME if it already exists). Returns true on success, false on failure.
251 =item Counter FILENAME
253 Increases the contents of FILENAME by one and returns the new value. Returns undef on failure. Fails silently.
255 You are visitor number <:= Counter('counter.txt') :>.
259 Replaces URLs (actually, replace things that look like URLs) by links.
261 In void context, B<changes> the value of the given variable. In other contexts, returns the changed version.
263 <: print AutoURL(Entity($user_input)); :>
265 =item AddCookie STRING
267 Adds a Set-Cookie header. STRING must be a valid Set-Cookie header value.
273 Juerd Waalboer <juerd@cpan.org>
275 Current maintainer: Mischa POSLAWSKY <shiar@cpan.org>