Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Right idea: you probably are meaning to use coderefs.

The way I've been using coderefs is as follows:

sub do_stat { my ( $dice, $mod ) = @_; my $roll = RPG::Dice::roll ( $dice ); return &$mod( $roll ); }
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:
# 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 );
(The \& here creates a reference to the function 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:

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 } ) ); }
Dr. Michael K. Neylon - || "You've left the lens cap of your mind on again, Pinky" - The Brain

In reply to Re: Modules: Building blocks for the Daft Adventure by Masem
in thread Modules: Building blocks for the Daft Adventure by Tiefling

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2022-05-26 02:58 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (92 votes). Check out past polls.