/usr/share/perl5/Petal/Utils.pm is in libpetal-utils-perl 0.06-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | package Petal::Utils;
=head1 NAME
Petal::Utils - Useful template modifiers for Petal.
=head1 SYNOPSIS
# install the default set of Petal modifiers:
use Petal::Utils;
# you can also install modifiers manually:
Petal::Utils->install( 'some_modifier', ':some_set' );
# see below for modifiers available & template syntax
=cut
use 5.006;
use strict;
use warnings::register;
use Petal::Hash;
our $VERSION = '0.06';
our $DEBUG = 0;
#------------------------------------------------------------------------------
# Cusomized import() so the user can select different plugins & sets
# use an Exporter-like syntax here:
our %PLUGIN_SET =
(
':none' => [],
':all' => [qw( :default :hash :debug )],
':default' => [qw( :text :date :logic :list :uri )],
':text' => [qw( UpperCase LowerCase UC_First Substr Printf )],
':logic' => [qw( And If Or Equal Like Decode )],
':date' => [qw( Date US_Date )],
':list' => [qw( Sort Limit Limitr)],
':hash' => [qw( Each Keys )],
':uri' => [qw( UriEscape Create_Href )],
':debug' => [qw( Dump )],
);
sub import {
my $class = shift;
push @_, ':default' unless @_;
return $class->install( @_ );
}
sub install {
my $class = shift;
foreach my $item (@_) {
next unless $item;
if ($item =~ /\A:/) {
$class->install_plugin_set( $item );
} else {
$class->install_plugin( $item );
}
}
return $class;
}
sub install_plugin_set {
my $class = shift;
my $set = shift;
my $plugins = $PLUGIN_SET{$set}
|| die "Can't install non-existent plugin set '$set'!";
# recursive so we can have sets of sets:
$class->install( @$plugins );
}
sub install_plugins {
my $class = shift;
map { $class->install_plugin( $_ ) } @_;
return $class;
}
sub install_plugin {
my $class = shift;
my $name = shift;
my $plugin = $class->find_plugin( $name );
warn "installing Petal plugin: '$name'\n" if $DEBUG;
if (UNIVERSAL::can($plugin, 'install')) {
$plugin->install;
} else {
$Petal::Hash::MODIFIERS->{"$plugin:"} = $plugin;
}
return $class;
}
sub find_plugin {
my $class = shift;
my $plugin = shift;
return \&$plugin if $class->can( $plugin );
if (my $plugin_class = $class->load_plugin( $plugin )) {
return $plugin_class;
}
die "Can't find Petal plugin: '$plugin'!";
}
sub load_plugin {
my $class = shift;
my $plugin = shift;
my $plugin_class = $class->get_plugin_class_for( $plugin );
return $plugin_class if $plugin_class->can( 'process' );
eval "require $plugin_class";
if ($@) {
warnings::warn("error loading $plugin plugin: $@") if warnings::enabled;
return;
}
return $plugin_class;
}
sub get_plugin_class_for {
my $class = shift;
my $plugin = shift;
my $plugin_class = "$class\::$plugin";
}
#------------------------------------------------------------------------------
# Plugins
## See Petal::Utils::<plugin> for plugin classes
## (plugins are now loaded as needed)
## Alternatively, use subs to insert new modifiers into the Petal Modifiers
## hash. Note that we do not get the $class value in this format.
# This style is deprecated:
# sub foo {
# my $hash = shift;
# my $args = shift;
# my $result = $hash->fetch( $args );
# return 'foo '.$result;
# }
1;
__END__
=head1 DESCRIPTION
The Petal::Utils package contains commonly used L<Petal> modifiers (or plugins),
and bundles them with an easy-to-use installation interface. By default, a
set of modifiers are installed into Petal when you use this module. You can
change which modifiers are installed by naming them after the use statement:
# use the default set:
use Petal::Utils qw( :default );
# use the date set of modifiers:
use Petal::Utils qw( :date );
# use only named modifiers, plus the debug set:
use Petal::Utils qw( UpperCase Date :debug );
# don't install any modifiers
use Petal::Utils qw();
You'll find a list of plugin sets throughout this document. You can also get
a complete list by looking at the variable:
%Petal::Utils::PLUGIN_SET;
For details on how the plugins are installed, see the "Advanced Petal" section
of the L<Petal> documentation.
=head1 MODIFIERS
Each modifier is listed under the set it belongs to.
=head2 :text
=over 4
=item lowercase:, lc: $string
Make the entire string lowercase.
<p tal:content="lc: $string">lower</p>
=item uppercase:, uc: $string
Make the entire string uppercase.
<p tal:content="uc: $string">upper</p>
=item uc_first: $string
Make the first letter of the string uppercase.
<p tal:content="uc_first: $string">uc_first</p>
=item substr: $string [offset] [length] [ellipsis]
Extract a substring from a string. Optionally add an ellipsis (...) to the
end. See also, perldoc -f substr.
<span petal:content="substr:$str">string</span> # does nothing
<span petal:content="substr:$str 2">string</span> # cuts the first two chars
<span petal:content="substr:$str 2 5">string</span> # extracts chars 2-7
<span petal:content="substr:$str 2 5 1">string with ellipsis</span> # same as above and adds an ellipsis
=item printf: format list
The printf modifier acts exactly like Perl's sprintf function to print
formatted strings.
<p petal:content="printf:'%s' 'Astro'">Astro</p>
<p petal:content="printf:'$%0.2f' '2.5'">$2.50</p>
=back
=head2 :date
=over 4
=item date: $date
Convert a time() integer to a date string using L<Date::Format>.
<span tal:replace="date: $date">Jan 1 1970 01:00:01</span>
=item us_date: $date
Convert an international date stamp (e.g., yyyymmdd, yyyy-mm-dd, yyyy/mm/dd)
to US format (mm/dd/yyyy).
<p tal:content="us_date: $date">2003-09-05</p>
=back
=head2 :logic
=over 4
=item if: $expr1 then: $expr2 else: $expr3
Do an if/then/else test and return the value of the expression executed.
Truthfulness of $expr1 is according to Perl (e.g., non-zero, non-empty string).
<p tal:attributes="class if: on_a_page then: a_class else: another_class">
Some text here...
</p>
=item or: $expr1 $expr2
Do a logical or. Truthfulness is according to Perl (e.g., non-zero, non-empty
string).
<p tal:if="or: $first $second">
first or second = <span tal:replace="or: $first $second">or</span>
</p>
=item and: $expr1 $expr2
Do a logical and. Truthfulness is according to Perl (e.g., non-zero, non-empty
string).
first and second = <span tal:replace="and: $first $second">and</span>
=item equal:, eq: $expr1 $expr2
Test for equality. Numbers are compared with C<==>, strings with C<eq>.
Truthfulness is according to Perl (e.g., non-zero, non-empty string).
first eq second = <span tal:replace="eq: $first $second">equal</span>
=item like: $expr $regex
Test for equality to a regular expression (see L<perlre>).
name like regex = <span tal:replace="like: $name ^Will.+m">like</span>
=item decode, decode: expression search result [search result]... [default]
The decode function has the functionality of an IF-THEN-ELSE statement. A
case-sensitive regex comparison is performed. All text strings must be
enclosed in single quotes.
'expression' is the value to compare.
'search' is the value that is compared against expression.
'result' is the value returned, if expression is equal to search.
'default. is optional. If no matches are found, the decode will return
default. If default is omitted, then the decode statement will return
null (if no matches are found).
<p petal:content="decode:$str 'dog' 'Satchel'">100</p> # if $str = dog, returns Satchel
<p petal:content="decode:$str 'cat' 'Buckey' 'Satchel'">Astro</p> # if $str = cat, returns Buckey, else Satchel
=back
=head2 :list
=over 4
=item sort: $list
Sort the values in a list before returning it.
<ul>
<li tal:repeat="item sort: $array_ref">$item</li>
</ul>
=item limit: $list count
Limit the values in a list before returning it.
<ul>
<li tal:repeat="item limit: $array_ref 2">$item</li>
</ul>
=item limitr: $list count
Shuffle the list then limit the returned values to the specified count.
<ul>
<li tal:repeat="item limitr: $array_ref 2">$item</li>
</ul>
=back
=head2 :hash
=over 4
=item keys: $hash
Return a list of keys for a hashref. Note: It appears that values cannot be
accessed with dynamic keys. If you need the keys and values, use "each:".
<ul>
<li tal:repeat="key keys: $hash_ref"><span tal:replace="key">key</span></li>
</ul>
=item each: $hash
Return a list of key/value pairs for a hashref.
<ul>
<li tal:repeat="item each: $hash_ref">
<span tal:replace="item/key">key</span> => <span tal:replace="item/val">value</span>
</li>
</ul>
=back
=head2 :uri
=over 4
=item uri_escape: $expr
Use L<URI::Escape>'s uri_escape() to escape the return value of the expression.
<a href="http://foo/get.html?item=${uri_escape: item/key}">get $item/key</a>
=item create_href: $url [protocol]
Creates an absolute uri from a url with the given protocol (e.g., http, ftp --
do not include the protocol separators). If the url does not have the
protocol included, it will be appended. If no protocol is given, 'http' will
be used.
<a petal:attr="href create_href:$url">HTTP Link</a>
<a petal:attr="href create_href:$url ftp">FTP Link</a>
=back
=head2 :debug
=over 4
=item dump: $expr
Dump the data strcture of the value given.
dump name: <span tal:replace="dump: name">dump</span>
=back
=head1 SUPERSETS
At the time of writing, the following supersets were available:
':none' => [],
':all' => [qw( :default :hash :debug )],
':default' => [qw( :text :date :logic :list )],
See C<%Petal::Utils::PLUGIN_SET> for an up-to-date list.
=head1 CONTRIBUTING
Contributions to the modifiers are welcome! You can suggest new modifiers to
add to the suite. You will have better luck getting your modifier added by
providing a module (see lib/Petal/Utils/And.pm for an example), a patch to
Utils.pm (with a modified PLUGIN_SET and documentation for your new modifier),
and a test suite. All modifiers are subject to the discretion of the authors.
=head1 AUTHORS
William McKee <william@knowmad.com>, and Steve Purkis <spurkis@cpan.org>
=head1 COPYRIGHT
Copyright (c) 2003-2004 William McKee & Steve Purkis.
This module is free software and is distributed under the same license as Perl
itself. Use it at your own risk.
=head1 THANKS
Thanks to Jean-Michel Hiver for making L<Petal> available to the Perl community.
=head1 SEE ALSO
L<Petal>
=cut
|