Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Dynamic function chains?

by broquaint (Abbot)
on Sep 15, 2004 at 12:48 UTC ( #391148=note: print w/ replies, xml ) Need Help??


in reply to Dynamic function chains?

Just put the functions in a package and call them dynamically e.g

{ package My::Maths; sub power { return exp( $_[0] ) } sub log2 { return log( $_[0] )/log(2.0) } sub loge { return log( $_[0] ) } sub log10 { return log( $_[0] )/log(10.0) } sub round { my $n = shift; return int($n + 0.5) if ($n >= 0); return int($n - 0.5); } sub trunc { return int($_[0]) } sub f1 { return sprintf "%.1f", $_[0] } sub f2 { return sprintf "%.2f", $_[0] } sub f3 { return sprintf "%.3f", $_[0] } sub f4 { return sprintf "%.4f", $_[0] } sub f5 { return sprintf "%.5f", $_[0] } } my $name = shift or die "Usage: $0 FUNC\n"; die "$0: unknown function method\n" unless my $func = My::Maths->can($method); while (<STDIN> ) { chomp; my @in = split; print shift(@in), " "; print join(" ", map &$func($_), @in), "\n"; }
HTH

_________
broquaint


Comment on Re: Dynamic function chains?
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://391148]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2015-07-03 16:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (54 votes), past polls