I've included a patch that adds several vmethods that are currently in 
CGI::Ex::Template that I have found very, very useful.  I've included the pod 
for the new methods in the body of this email for discussion.  The patch 
includes code, pod, and tests for each of the new methods.

Paul

New Text VMethods
> =item '0'
>
> Essentially the same thing as the item vmethod.  Allows for items
> to truly behave like lists - without having to call the list
> vmethod.
>
>     [% thing = 'foo' %][% thing.0 %]
>
> =item int
>
> Return the integer portion of the value (0 if none).
>
>     [% thing.int %]
>
> =item fmt
>
> Returns a string formatted with the passed pattern.  Default pattern is %s.
>
> Similar to the Perl6 fmt method for scalars.  Also similar to the format 
filter,
> but doesn't split the string on newlines like format does.
>
>     [% thing.fmt('%d') %]
>     [% thing.fmt('%6s') %]
>     [% thing.fmt('%*s', 6) %]
>
> =item rand
>
> Returns a random number greater or equal to 0 but less than the
> item itself.
>
>     [% thing = 10; thing.rand %]

New Hash VMethods

> =item fmt
>
> Passed a pattern and an string to join on.  Returns a string of the 
key/value pairs
> of the hash formatted with the passed pattern and joined with the passed 
string.
> Default pattern is "%s\t%s" and the default join string is a newline.
>
> Similar to the Perl6 fmt method for hashes.
>
>     [% myhash.fmt('%s => %s', "\n") %]
>     [% myhash.fmt('%4s => %5s', "\n") %]
>     [% myhash.fmt('%*s => %*s', "\n", 4, 5) %]

New List VMethods

> =item fmt
>
> Passed a pattern and an string to join on.  Returns a string of the values 
of the list
> formatted with the passed pattern and joined with the passed string.
> Default pattern is %s and the default join string is a space.
>
> Similar to the Perl6 fmt method for arrays.
>
>     [% mylist.fmt('%s', ', ') %]
>     [% mylist.fmt('%6s', ', ') %]
>     [% mylist.fmt('%*s', ', ', 6) %]
>
> =item pick
>
> Returns a random item from the list.  If a numeric value is
> passed and is greater than one, then a list of random
> values from the array is returned (Note: the same value
> may be returned multiple times as each pick is random).
>
>     [% mylist = ['a' .. 'z'] %]
>     [% mylist.pick %]
>     [% mylist.pick(8).join('') %]
diff -r Template-Toolkit-2.19/lib/Template/Manual/VMethods.pod Template-Toolkit-2.19-vmethods/lib/Template/Manual/VMethods.pod
211a212,243
> =item '0'
> 
> Essentially the same thing as the item vmethod.  Allows for items
> to truly behave like lists - without having to call the list
> vmethod.
> 
>     [% thing = 'foo' %][% thing.0 %]
> 
> =item int
> 
> Return the integer portion of the value (0 if none).
> 
>     [% thing.int %]
> 
> =item fmt
> 
> Returns a string formatted with the passed pattern.  Default pattern is %s.
> 
> Similar to the Perl6 fmt method for scalars.  Also similar to the format filter,
> but doesn't split the string on newlines like format does.
> 
>     [% thing.fmt('%d') %]
>     [% thing.fmt('%6s') %]
>     [% thing.fmt('%*s', 6) %]
> 
> =item rand
> 
> Returns a random number greater or equal to 0 but less than the
> item itself.
> 
>     [% thing = 10; thing.rand %]
> 
371a404,415
> =item fmt
> 
> Passed a pattern and an string to join on.  Returns a string of the key/value pairs
> of the hash formatted with the passed pattern and joined with the passed string.
> Default pattern is "%s\t%s" and the default join string is a newline.
> 
> Similar to the Perl6 fmt method for hashes.
> 
>     [% myhash.fmt('%s => %s', "\n") %]
>     [% myhash.fmt('%4s => %5s', "\n") %]
>     [% myhash.fmt('%*s => %*s', "\n", 4, 5) %]
> 
578a623,645
> =item fmt
> 
> Passed a pattern and an string to join on.  Returns a string of the values of the list
> formatted with the passed pattern and joined with the passed string.
> Default pattern is %s and the default join string is a space.
> 
> Similar to the Perl6 fmt method for arrays.
> 
>     [% mylist.fmt('%s', ', ') %]
>     [% mylist.fmt('%6s', ', ') %]
>     [% mylist.fmt('%*s', ', ', 6) %]
> 
> =item pick
> 
> Returns a random item from the list.  If a numeric value is
> passed and is greater than one, then a list of random
> values from the array is returned (Note: the same value
> may be returned multiple times as each pick is random).
> 
>     [% mylist = ['a' .. 'z'] %]
>     [% mylist.pick %]
>     [% mylist.pick(8).join('') %]
> 
diff -r Template-Toolkit-2.19/lib/Template/VMethods.pm Template-Toolkit-2.19-vmethods/lib/Template/VMethods.pm
37a38,41
>     '0'     => \&text_0,
>     fmt     => \&text_fmt,
>     int     => \&text_int,
>     rand    => \&text_rand,
54a59
>     fmt     => \&hash_fmt,
72a78,79
>     fmt     => \&list_fmt,
>     pick    => \&list_pick,
118a126,147
> sub text_0 {
>     $_[0];
> }
> 
> sub text_fmt {
>     my $str = shift; $str = ''   if ! defined $str;
>     my $pat = shift; $pat = '%s' if ! defined $pat;
>     no warnings;
>     return @_ ? sprintf($pat, $_[0], $str)
>               : sprintf($pat, $str);
> }
> 
> sub text_int {
>     no warnings;
>     int $_[0];
> }
> 
> sub text_rand {
>     no warnings;
>     rand $_[0];
> }
> 
270a300,309
> sub hash_fmt {
>     my $ref = shift || return '';
>     my $pat = shift; $pat = "%s\t%s" if ! defined $pat;
>     my $sep = shift; $sep = "\n"     if ! defined $sep;
>     no warnings;
>     return ! @_    ? join($sep, map {sprintf $pat, $_, $ref->{$_}} sort keys %$ref)
>          : @_ == 1 ? join($sep, map {sprintf $pat, $_[0], $_, $ref->{$_}} sort keys %$ref) # don't get to pick - it applies to the key
>          :           join($sep, map {sprintf $pat, $_[0], $_, $_[1], $ref->{$_}} sort keys %$ref);
> }
> 
362a402,418
> sub list_fmt {
>     my $ref = shift || return '';
>     my $pat = shift; $pat = '%s' if ! defined $pat;
>     my $sep = shift; $sep = ' '  if ! defined $sep;
>     no warnings;
>     return @_ ? join($sep, map {sprintf $pat, $_[0], $_} @$ref)
>               : join($sep, map {sprintf $pat, $_} @$ref);
> }
> 
> sub list_pick {
>     my $ref = shift;
>     no warnings;
>     my $n   = int(shift);
>     $n = 1 if $n < 1;
>     my @ind = map { $ref->[ rand @$ref ] } 1 .. $n;
>     return $n == 1 ? $ind[0] : [EMAIL PROTECTED];
> }
diff -r Template-Toolkit-2.19/t/vmethods/hash.t Template-Toolkit-2.19-vmethods/t/vmethods/hash.t
28a29
>     h         => {b => "B", c => "C"},
127c128,142
< 
---
> -- test --
> -- name hash.fmt --
> [% h.fmt %]
> [% h.fmt('%s => %s') %]
> [% h.fmt('%s => %s', '|') %]
> [% h.fmt('%*s=>%s', '|', 3) %]
> [% h.fmt('%*s=>%*s', '|', 3, 4) %]
> --expect--
> b	B
> c	C
> b => B
> c => C
> b => B|c => C
>   b=>B|  c=>C
>   b=>   B|  c=>   C
diff -r Template-Toolkit-2.19/t/vmethods/list.t Template-Toolkit-2.19-vmethods/t/vmethods/list.t
91a92,93
>     a_list     => [2, 3],
>     b_list     => [3],
395c397,417
< 
---
> -- test --
> -- name list.fmt --
> [% a_list.fmt %]
> [% a_list.fmt('%02d') %]
> [% a_list.fmt('%02d',' ') %]
> [% a_list.fmt('%02d','|') %]
> [% a_list.fmt('%0*d','|', 3) %]
> -- expect --
> 2 3
> 02 03
> 02 03
> 02|03
> 002|003
> 
> -- test --
> -- name list.pick --
> [% b_list.pick %]
> [% b_list.pick(5).join('') %]
> -- expect --
> 3
> 33333
diff -r Template-Toolkit-2.19/t/vmethods/text.t Template-Toolkit-2.19-vmethods/t/vmethods/text.t
51a52,58
> 
>     num0     => 7,
>     num1     => '123.234',
>     num2     => "123gggg",
>     num3     => "ff123.234",
>     text1    => "a\nb",
>     is_num   => sub { defined($_[0]) && $_[0] =~ /^\d+(?:|\.\d+)$/ ? "Is numberish" : "Is not numberish" },
358a366,402
> -- test --
> -- name text.0 --
> [% animal.size %] [% animal.0 %]
> -- expect --
> 1 cat
> 
> -- test --
> -- name text.int --
> [% num1.int %]
> [% num2.int %]
> [% num3.int %]
> -- expect --
> 123
> 123
> 0
> 
> -- test --
> -- name text.fmt --
> [% num0.fmt %]
> [% num0.fmt('%02d') %]
> [% num0.fmt('%0*d', 3) %]
> [% text1.fmt('(%s)') %]
> -- expect --
> 7
> 07
> 007
> (a
> b)
> 
> -- test --
> -- name text.rand --
> [% is_num(num1.rand) %]
> [% is_num(animal.rand) %]
> -- expect --
> Is numberish
> Is numberish
> 

Reply via email to