Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

Comment on Re: sub and anonymous sub
Download Code
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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2016-04-30 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    :nehw tseb si esrever ni gnitirW







    Results (441 votes). Check out past polls.