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

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

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.