On Tue, 18 Jul 2006 13:18:32 -0400 Todd Freeman <[EMAIL PROTECTED]> wrote:
> I just made my own rand vmethod (yes it's ugly... but I do take
> suggestions :} )
> ============
How about (edited below)
> package Template::VMethod::Rand;
>
> use base qw(Template::Plugin);
> use strict;
>
> sub load {
> my ( $class, $context ) = @_;
> # Set this twice to avoid a "used only once" warning
> $Template::Stash::LIST_OPS->{rand} = \&randomizer;
> $Template::Stash::LIST_OPS->{rand} = \&randomizer;
Just do it once with
{
no warnings;
$Template::Stash::LIST_OPS->{rand} = \&randomizer;
}
> return $class;
> }
>
> sub randomizer
> { return $_[0][int(rand($#{$_[0]}+1))]; }
{ my $arr = shift; return $arr->[rand @$arr] }
[] already forces int, and scalar is much neater than $# + 1
> 1;
--
Paul "LeoNerd" Evans
[EMAIL PROTECTED]
ICQ# 4135350 | Registered Linux# 179460
http://www.leonerd.org.uk/
signature.asc
Description: PGP signature
