1 package PLP::Functions;
7 our @EXPORT = qw/Entity DecodeURI EncodeURI Include include PLP_END
8 AddCookie ReadFile WriteFile AutoURL Counter exit/;
16 eval 'package PLP::Script; ' . PLP::source($PLP::file, 0, join ' ', (caller)[2,1]);
18 PLP::Functions::exit() if $@ =~ /\cS\cT\cO\cP/;
32 push @PLP::END, shift;
36 my $ref = defined wantarray ? [@_] : \@_;
44 s/\t/ /g;
48 return defined wantarray ? (wantarray ? @$ref : "@$ref") : undef;
52 my $ref = defined wantarray ? [@_] : \@_;
55 tr/+/ /; # Browsers do tr/ /+/ - I don't care about RFCs, but
56 # I do care about real-life situations.
57 s/%([0-9A-Fa-f][0-9A-Fa-f])/chr hex $1/ge;
60 return defined wantarray ? (wantarray ? @$ref : "@$ref") : undef;
64 my $ref = defined wantarray ? [@_] : \@_;
67 s{([^A-Za-z0-9\-_.!~*'()/?:@\$,])}{sprintf("%%%02x", ord $1)}ge;
70 return defined wantarray ? (wantarray ? @$ref : "@$ref") : undef;
74 if ($PLP::Script::header{'Set-Cookie'}) {
75 $PLP::Script::header{'Set-Cookie'} .= "\nSet-Cookie: $_[0]";
77 $PLP::Script::header{'Set-Cookie'} = $_[0];
83 open (my $fh, '<', $_[0]) or do {
84 PLP::error("Cannot open $_[0] for reading ($!)", 1);
93 open (my $fh, '>', $_[0]) or do {
94 PLP::error("Cannot open $_[0] for writing ($!)", 1);
98 print $fh $_[1] or do {
99 PLP::error("Cannot write to $_[0] ($!)");
103 PLP::error("Cannot close $_[0] ($!)");
112 open $fh, '+<', $_[0] or
113 open $fh, '>', $_[0] or return undef;
119 print $fh ++$counter or return undef;
120 close $fh or return undef;
125 # This sub assumes your string does not match /(["<>])\cC\1/
126 my $ref = defined wantarray ? \(my $copy = $_[0]) : \$_[0];
128 $$ref =~ s/"/"\cC"/g; # Single characters are easier to match :)
129 $$ref =~ s/>/>\cC>/g; # so we can just use a character class []
130 $$ref =~ s/</<\cC</g;
132 # Now this is a big, ugly regex! But hey - it works :)
133 $$ref =~ s{((\w+://|www\.|WWW\.)[a-zA-Z0-9\.\@:-]+[^\"\'>< \r\t\n]*)}{
136 s/// if (my $trailing) = /([\.,!\?\(\)\[\]]+$)/;
137 s/&(?!\x23?\w+;)/&/g;
139 my $href = ($scheme =~ /www\./i ? "http://$_" : $_);
140 qq{<a href="$href" target="_blank">$_</a>$trailing};
143 $$ref =~ s/"\cC"/"/g;
144 $$ref =~ s/>\cC>/>/g;
145 $$ref =~ s/<\cC</</g;
147 if ($@){ return defined wantarray ? @_ : undef } # return original on error
148 return defined wantarray ? $$ref : undef;
155 PLP::Functions - Functions that are available in PLP documents
159 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.
161 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>.
163 Some context examples:
165 print foo(); # foo is in list context (print LIST)
166 foo(); # foo is in void context
167 $bar = foo(); # foo is in scalar context
168 @bar = foo(); # foo is in list context
169 length foo(); # foo is in scalar context (length EXPR)
175 =item Include FILENAME
177 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).
179 Include can be used recursively, and there is no depth limit:
181 <!-- This is crash.plp -->
184 # This example will loop forever,
185 # and dies with an out of memory error.
186 # Do not try this at home.
189 =item include FILENAME
191 An alias for C<Include>.
195 Adds a piece of code that is executed when at the end of the PLP document. This is useful when creating a template file:
197 <html><body> <!-- this is template.plp -->
202 <(template.plp)> <!-- this is index.plp -->
205 You should use this function instead of Perl's built-in C<END> blocks, because those do not work properly with mod_perl.
209 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.
211 In void context, B<changes> the values of the given variables. In other contexts, returns the changed versions.
213 <: print Entity($user_input); :>
215 Be warned that this function also HTMLizes consecutive whitespace and newlines (using and <br> respectively).
216 For simple escaping, use L<XML::Quote>. To escape high-bit characters as well, use L<HTML::Entities>.
220 Encodes URI strings according to RFC 3986. All disallowed characters are replaced by their %-encoded values.
222 In void context, B<changes> the values of the given variables. In other contexts, returns the changed versions.
224 <a href="/foo.plp?name=<:= EncodeURI($name) :>">Link</a>
226 Note that the following reserved characters are I<not> percent-encoded, even though they may have a special meaning in URIs:
230 This should be safe for escaping query values (as in the example above), but it may be a better idea to use L<URI::Escape> instead.
234 Decodes %-encoded strings. Unlike L<URI::Escape>, it also translates + characters to spaces (as browsers use those).
236 In void context, B<changes> the values of the given variables. In other contexts, returns the changed versions.
238 =item ReadFile FILENAME
240 Returns the contents of FILENAME in one large string. Returns undef on failure.
242 =item WriteFile FILENAME, STRING
244 Writes STRING to FILENAME (overwrites FILENAME if it already exists). Returns true on success, false on failure.
246 =item Counter FILENAME
248 Increases the contents of FILENAME by one and returns the new value. Returns undef on failure. Fails silently.
250 You are visitor number <:= Counter('counter.txt') :>.
254 Replaces URLs (actually, replace things that look like URLs) by links.
256 In void context, B<changes> the value of the given variable. In other contexts, returns the changed version.
258 <: print AutoURL(Entity($user_input)); :>
260 =item AddCookie STRING
262 Adds a Set-Cookie header. STRING must be a valid Set-Cookie header value.
268 Juerd Waalboer <juerd@cpan.org>
270 Current maintainer: Mischa POSLAWSKY <shiar@cpan.org>