Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Re (tilly) 1: The zen of code blocks

by mdupont (Scribe)
on Aug 23, 2001 at 18:18 UTC ( #107333=note: print w/ replies, xml ) Need Help??


in reply to Re (tilly) 1: The zen of code blocks
in thread The zen of code blocks

#

# a perl koan designed for a true perl monk use warnings; use strict; my $this; # do I need this and that? my $that; # tell me oh perl monks! # a structured way of thinking, # the params is a way to pass the parameters # and the thought is what to do with them sub koan { my $params = shift; # the code ref is a parameter # that defines the parameters my $thought = shift; # the code ref that # defines the work return sub { &$params(@_); # Get the parameters, # from @_ and make them # available in thought &$thought; # Do the work using these parameters } }; # create a closure to think about the thought sub zen { my $thought = shift; my @params = @_; return sub { &$thought(@params); # call with the saved params }; }; sub integer_parm # called to get a simple integer { my $val= shift; # the value passes my $type = ref $val; # get the type of param return $val if(!$type); # return simple values if ($type eq "CODE") { $val = &$val; # eval the parameter } else { die "no refs in this"; # dont allow pointers } die unless $val =~ /^\d+$/; # the value of the param must be an integer return $val; } ############################################### # different thoughts to meditate on # types of thoughts my $binary_thought = sub { $this = shift; # now I want to have # this available in koan # and to the thought, $that = shift; # but not to be overwritten # if called recursivly $this=integer_parm($this); $that=integer_parm($that); }; my $add_func = sub { $this + $that; }; my $minus_func = sub { $this - $that; }; # different thoughts my $add_koan = koan($binary_thought,$add_func); my $minus_koan = koan($binary_thought,$minus_func); ## SOME EXAMPLES print &$add_koan(1,1); # direct thought my $thought = zen($add_koan,1,2); # indirect thought print &$thought . "\n"; print &$add_koan(1,$thought) . "\n"; # tree of thoughts #mdupont #


Comment on Re: Re (tilly) 1: The zen of code blocks
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-09-17 03:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (57 votes), past polls