Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

The zen of code blocks

by mdupont (Scribe)
on Aug 23, 2001 at 15:49 UTC ( #107290=poem: print w/ replies, xml ) Need Help??

# a perl koan designed for a true perl monk sub zen { my $coderef = shift; # the code ref is a &$coderef; # Find the inner meaning }; zen (sub { print "How can I put a variable that is only visable to zen from within this anon code block?"; }); # mdupont

Comment on The zen of code blocks
Download Code
Re (tilly) 1: The zen of code blocks
by tilly (Archbishop) on Aug 23, 2001 at 16:08 UTC
    sub zen { my $sub = shift; &$sub; } { my $answer = "Use a closure\n"; zen(sub{print $answer;}); }
      LMAO!
      I have no idea if it was intended to be funny at all, but I think that it was a brilliant question and a brilliant answer. It really made me laugh (in a happy way, not derogative at all). What can I say: ++ guys :)

      --
      Graq

      #
      # 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 #
Re: The zen of code blocks
by Zaxo (Archbishop) on Aug 24, 2001 at 00:30 UTC

    I looks like I'm long scooped, but this was too much fun to hurry :-) Thanks for the super question!

    #!/usr/bin/perl -w use strict; sub zen { my $coderef = shift; # the code ref is a &$coderef; # Find the inner meaning[s] # &$coderef; }; my $petition = "How can I put a variable that is only visible to zen f +rom within this anon code block?"; zen do { my $speech = $petition; print $speech,$/; sub { $_ = shift; $speech = $speech =~ /^How/ ? $_ : "Mu."; print $speech,$/; } }, "There is do and there is not do.";

    Update: Replaced tabs to fix formatting.

    After Compline,
    Zaxo

Re: The zen of code blocks
by Anonymous Monk on Aug 30, 2001 at 14:34 UTC
    Hey Mike.
    # a perl koan designed for a true perl monk sub zen { my $coderef = shift; # the code ref is a &$coderef; # Find the inner meaning }; zen (sub { print "How can I put a variable that is only visable to zen from within this anon code block?"; }); # mdupont
    Mikey... Thats not an anonymous code block, its a anonymous sub (closure). Theres a difference.
    Anon blocks don't return, subs do.
    Loop controls dont work in subs, they do in anon blocks.
    You can have a reference to a sub but not an anon block.
    An anon block can have a LABEL: a sub cannot (hmm, well a named sub IS a label, but a closure cant)
    sub Alpha { my $param=shift; { # this is an anonymous code block return; #this leaves the sub, not just the block } print "Do you see me?\n"; } sub Bravo { my $n=shift; ANONYMOUS:{ #print "$n\n"; last ANONYMOUS if !$n; $n-- && redo ANONYMOUS if $n>10; print "$n\n"; $n-- && redo ANONYMOUS if $n>5; } } Alpha; Bravo(20);

    And the style &$coderef is inclined towards errors (and basically deprecated except when taking a reference to a sub), $coderef->() is much easier to understand.

    Anyway heres my answer to your koan...

    sub zen { my $art=shift; my $motorcycle=$art->('of'); print $$motorcycle.'='.(caller(0))[3]; } zen sub{return \'maintainance' if $_[0]=='of'};

    HTH
    Yves
      Mikey if you want to know who wrote that it was me.
      Newbie here and I didnt realize that I wasnt already logged in. :-)
      Sorry
      Yves
      Set your background theme to something other than the default. That will make it more obvious whether or not you are logged in, preventing this kind of error.
        Umm. I suppose I should RTFM, but how do I do that tilly? (And thanks for the suggestion)

        Oh and it was cause I was doing a IE update in the background and for some reason decided that it wouldnt deal with cookies until i rebooted. Sigh. Thanks bill.
        yves

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-07-30 23:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (241 votes), past polls