in reply to The zen of code blocks

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

Replies are listed 'Best First'.
Re: Re: The zen of code blocks
by Anonymous Monk on Aug 30, 2001 at 14:44 UTC
    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
Re (tilly) 2: The zen of code blocks
by tilly (Archbishop) on Aug 30, 2001 at 15:27 UTC
    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