I just made my own rand vmethod (yes it's ugly... but I do take
suggestions :} )
============

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;
        
        return $class;
        }

sub randomizer
        { return $_[0][int(rand($#{$_[0]}+1))]; }
1;
============

On Tue, Jul 18, 2006 at 06:02:17PM +0100, Gavin Henry wrote:
> Dear all,
> 
> I am trying to get a random image/"Tip of the Day" from a list of
> images/tips for one of our sites. I was playing naively with:
> 
> [% USE Math %]
> 
> [% tips = [ 'test'
> 
>               'test1'
> 
>               'test2'
> 
>               'test3' ] %]
> 
> [% Math.rand(tips) %]
> 
> But in my eyes tips is an array ref, so it will be using the memory
> address of that array and giving me a random fractional number greater
> than or equal to 0 and less than the value of this address.
> 
> What's the proper way ;-)
> 
> Cheers,
> 
> Gavin.
> 
> -- 
> Walking the road to enlightenment... I found a penguin and a camel on the
> way..... Fancy a [EMAIL PROTECTED] Just ask!!!
> http://perlmonks.org/?node_id=386673
> 
> _______________________________________________
> templates mailing list
> [email protected]
> http://lists.template-toolkit.org/mailman/listinfo/templates

-- 
Todd Freeman  Ext 6103                   .^.    Don't fear the penguins!
Programming Department                   /V\
Andrews University                      // \\    http://www.linux.org/
http://www.andrews.edu/~freeman/       /(   )\   http://www.debian.org/
                                        ^^ ^^

Attachment: signature.asc
Description: Digital signature

Reply via email to