Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: sub and anonymous sub

by rinceWind (Monsignor)
on Jun 21, 2002 at 12:59 UTC ( #176268=note: print w/replies, xml ) Need Help??

in reply to sub and anonymous sub

Interestingly, I thinkl there is a cleaner way to do it. My experience of other programming languages is showing. The algorithm you have given is non-reentrant, hence non-threadsafe.

Instead, I prefer passing in a level number to recurse, which means that separate threads calling recurse will not interfere with one another.

#(1) sub recurse { #(2) my ($depth) = @_; print +(" .") x $depth . "current depth:$depth\n" ; recurse($depth + 1) if $depth < 3; print +(" .") x $depth . "current depth:$depth\n" ; } #(3) #(4) recurse(0); # (5)
I know perl is not thread safe, but perl 6 and/or perl 5.x might be.

My $0.02 --rW

Update: Gerbil is right. I don't need the extra braces. This is what comes with cutting and pasting someone else's example rather than rewriting from scratch.

Have edited the example to take on Gerbil's suggestions

Replies are listed 'Best First'.
Re: Re: sub and anonymous sub
by Gerbil (Initiate) on Jun 21, 2002 at 22:39 UTC
    I am far away from being a mighty Perl hacker, but I've done recursion depth counting this way all the time. It's "cleaner" because you don't have to understand Perl-specific techniques. And it's thread-safe, too? Cool ;) Didn't know that... But why keep the curly brackets?
    sub recurse { my ($depth) = @_; print +(" .") x $depth . "current depth:$depth\n" ; recurse($depth + 1) if $depth < 3; print +(" .") x $depth . "current depth:$depth\n" ; } recurse(-1);
    This is the same, right? Additionally, I would start with recurse(0);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://176268]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2018-06-24 00:22 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.