18 September 2008
This post may be outdated due to it was written on 2008. The links may be broken. The code may be not working anymore. Leave comments if needed.
for example I want a shuffled list everytime when I can $t->shuffled_list;
I know I can use a sub to do the request. but with Moose, it's much more elegant.

the main code is like:
around 'shuffled_list' => sub {
my $next = shift;

my $ret = $next->(@_);
$ret = [ shuffle( @$ret ) ];

return $ret;

full source code can be found in http://fayland.googlecode.com/svn/trunk/script/learn_moose/002_around.pl

the output looks like:
1, 10, 6, 4, 2, 7, 9, 8, 3, 5
3, 4, 7, 2, 1, 9, 5, 6, 8, 10
Here we use 'around' instead of 'before' or 'after' because 'before' doesn't work according to this FAQ: http://search.cpan.org/~drolsky/Moose-0.57/lib/Moose/Cookbook/FAQ.pod#How_can_I_affect_the_values_in_@__using_before?

the example above is a case of 'deflate' as http://search.cpan.org/~drolsky/Moose-0.57/lib/Moose/Cookbook/FAQ.pod#How_can_I_get_Moose_to_inflate/deflate_values_in_the_accessor?

please read the FAQ and Cookbook for more details.


blog comments powered by Disqus