Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: a loop for creating subroutines?

by LanX (Bishop)
on Aug 27, 2012 at 16:33 UTC ( #990017=note: print w/replies, xml ) Need Help??

in reply to a loop for creating subroutines?

IMHO the cleanest way is a dispatcher table:

$mysubs{name} = sub { #subroutine implementation }; ... $mysubs{name}->("call","args","whatever");


another approach with a dedicated package

*MySubs::name = sub { #subroutine implementation }; ... # and later MySubs::name("call","args","whatever"); # ... or ... package MySubs; name("call","args","whatever");

(also untested)


Cheers Rolf

Replies are listed 'Best First'.
Re^2: a loop for creating subroutines?
by mascip (Pilgrim) on Aug 27, 2012 at 16:43 UTC

    With this solution, how would my example code look like? Like this?

    $mysubs{light}->(10); $mysubs(light_at_time)->($timestamp);

      I've already shown you three possibilities to call with "arguments".

      I wouldn't try to load unknown subs into main-namespace, that can easily lead to errors.

      Try and see what fits your needs.

      Cheers Rolf

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://990017]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-07-18 20:43 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (395 votes). Check out past polls.