Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: recursive anonymous subroutines

by ambrus (Abbot)
on Apr 06, 2006 at 20:11 UTC ( #541716=note: print w/ replies, xml ) Need Help??

in reply to recursive anonymous subroutines

No. A code reference is the only way to use an anonymous subroutine.

Here's an example of a recursive anonymous sub from a code I wrote earlier:

# WRONG ... my $traverse; $traverse = sub { my(@c, $c, @m); @c = $_[0]; while (@c) { $c = pop @c; for (@{$member{$c}}) { push @m, $_; } for (@{$child{$c}}) { push @c, $_; } } @m; }; for (keys(%cut)) { my @m = &$traverse($_); my @m0 = grep { !$poison{$_} } @m; my @m1 = grep { $poison{$_} } @m; print "( ", jointab(@m0), "@ ", jointab(@m1), ")\n"; } ...

Update: as jdporter has noticed, this example isn't recursive. It uses a stack. I was mislead by the name "traverse". Sorry.

Comment on Re: recursive anonymous subroutines
Download Code
Re^2: recursive anonymous subroutines
by Daryn (Sexton) on Apr 06, 2006 at 20:58 UTC
    well the answers came even before I was able to update the original very incomplete post (first posting syndrome I guess). Many thanks to all.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2015-03-31 03:43 GMT
Find Nodes?
    Voting Booth?

    When putting a smiley right before a closing parenthesis, do you:

    Results (660 votes), past polls