# 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
#
|