The way I've been using coderefs is as follows:
That is, the &$mod is a function call of a deref'ed coderef. To actually call this, you can do it by one of two ways:sub do_stat { my ( $dice, $mod ) = @_; my $roll = RPG::Dice::roll ( $dice ); return &$mod( $roll ); }
(The \& here creates a reference to the function modify).# anonymous function call my $roll = do_stat( "3d6", sub { int($_[0]/2)-5; } ); # Named function call sub modify { int($_[0]/2)-5; } my $roll2 = do_stat( "3d6, \&modify );
What's nice about coderefs is that they then can be used as variables, and allow you to quickly do different functions depending on what you pass:
</CODE>my @data = ( { name => 'STR', dice => '3d6', modifier => sub { $_[0] + 2 } }, { name => 'INT', dice => '3d6', modifier => sub { $_[0] - 5 } } ); foreach ( @data ) { $_->{ roll } = &{ $_->{ modifier } }( RPG::Dice::roll( $_->{ dice } ) ); }
In reply to Re: Modules: Building blocks for the Daft Adventure
by Masem
in thread Modules: Building blocks for the Daft Adventure
by Tiefling
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |