* Eric Cholet <[EMAIL PROTECTED]> [2002-07-18 14:26]:
> --On Thursday, July 18, 2002 13:59:23 -0400 darren chamberlain
> <[EMAIL PROTECTED]> wrote:
>
> >I'd like to add a couple of new virtual methods to Template::Stash: merge,
> >which takes several lists and (non-destructively) merges them into one
> >list, and commify, a scalar op, stolen from perlfaq 5.
> >
> > [% big_number = 187439493280; big_number.commify %]
> > 187,439,493,280
>
> It'd be nice if one can choose the separator, e.g.
> [% big_number = 187439493280; big_number.commify(" ") %]
Nice:
[% big_number = 187439493280; big_number.commify(" ") %]
[% big_number = 187439493280; big_number.commify %]
187 439 493 280
187,439,493,280
Implemented as:
sub commify {
local $_ = shift;
my $c = shift || ",";
1 while s/^([-+]?\d+)(\d{3})/$1$c$2/;
return $_;
}
I didn't think of that, since I copied commify right from perlfaq5. :)
New patch attached.
(darren)
--
Trying to be happy is like trying to build a machine for which the
only specification is that it should run noiselessly.
--- Stash.pm.orig Thu Jul 18 13:19:14 2002
+++ Stash.pm Thu Jul 18 15:44:46 2002
@@ -89,6 +89,7 @@
return [ defined $split ? split($split, $str, @args)
: split(' ', $str, @args) ];
},
+ 'commify' => \&commify,
defined $SCALAR_OPS ? %$SCALAR_OPS : (),
};
@@ -190,6 +191,11 @@
return $list->[$#$list] unless @_;
return list_slice_vmeth($list, -$_[0], $_[0]);
},
+
+ 'merge' => sub {
+ my $list = shift;
+ return [ @$list, grep defined, map ref eq 'ARRAY' ? @$_ : undef, @_ ];
+ },
defined $LIST_OPS ? %$LIST_OPS : (),
};
@@ -236,6 +242,15 @@
}
+# stolen from perlfaq 5
+sub commify {
+ local $_ = shift;
+ my $c = shift || ",";
+ 1 while s/^([-+]?\d+)(\d{3})/$1$c$2/;
+ return $_;
+}
+
+
#========================================================================
# ----- CLASS METHODS -----
#========================================================================