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

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.

Replies are listed 'Best First'.
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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2017-09-20 01:11 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (230 votes). Check out past polls.