Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Perl: the Markov chain saw
 
PerlMonks  

Re: Recursion not working

by BrowserUk (Pope)
on Jul 28, 2012 at 19:19 UTC ( #984223=note: print w/ replies, xml ) Need Help??


in reply to Recursion not working

It looks like I'm loosing my directory handle but I can't figure out why.

DIR is a global filehandle. When you recurse, you re-use that same global directory handle and the previous level's use of it gets over-written.

You have two options:

  1. Use lexical handles: In place of DIR & FILE use:
    opendir( my $dh, $dir) or die $!; while ($file = readdir( $dh )) ... open( my $fh, $FullName) or die ...
  2. Or localise those global handles: Add the following to the top of your sub
    sub ScanDir { local( DIR, FILE ); ...

One requires minimum changes; the other is seen as the "Modern" option. Both will work.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


Comment on Re: Recursion not working
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2013-05-25 07:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (519 votes), past polls