[Templates] Lists and rand like
http://faq.perl.org/perlfaq4.html#How_do_I_select_a_ra
Paul LeoNerd Evans
leonerd@leonerd.org.uk
Tue, 18 Jul 2006 21:43:22 +0100
--Sig_3WKNrmwls.wFIXDc6Z0Sb/C
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: quoted-printable
On Tue, 18 Jul 2006 13:18:32 -0400
Todd Freeman <freeman@andrews.edu> wrote:
> I just made my own rand vmethod (yes it's ugly... but I do take
> suggestions :} )
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
How about (edited below)
> package Template::VMethod::Rand;
>=20
> use base qw(Template::Plugin);
> use strict;
>=20
> sub load {
> my ( $class, $context ) =3D @_;
> # Set this twice to avoid a "used only once" warning
> $Template::Stash::LIST_OPS->{rand} =3D \&randomizer;
> $Template::Stash::LIST_OPS->{rand} =3D \&randomizer;
Just do it once with
{
no warnings;
$Template::Stash::LIST_OPS->{rand} =3D \&randomizer;
}
> return $class;
> }
>=20
> sub randomizer
> { return $_[0][int(rand($#{$_[0]}+1))]; }
{ my $arr =3D shift; return $arr->[rand @$arr] }
[] already forces int, and scalar is much neater than $# + 1
> 1;
--=20
Paul "LeoNerd" Evans
leonerd@leonerd.org.uk
ICQ# 4135350 | Registered Linux# 179460
http://www.leonerd.org.uk/
--Sig_3WKNrmwls.wFIXDc6Z0Sb/C
Content-Type: application/pgp-signature; name=signature.asc
Content-Disposition: attachment; filename=signature.asc
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
iD8DBQFEvUfvqRXzCRLr5iQRAq48AJkBjZvPqdpZ0B5paJ7ois7gzO+CxACgnKr8
uTrYiKq+0i6pwt1yaY4RJOQ=
=TgIz
-----END PGP SIGNATURE-----
--Sig_3WKNrmwls.wFIXDc6Z0Sb/C--