On Fri, Apr 09, 2004 at 10:54:04AM +0300, Shachar Shemesh wrote: > Hi, > > I'm trying to create a perl program that will recurse into > subdirectories. I have: > > >sub scandir { > > local $dirname=shift; > > > > opendir DIR, $dirname or die "Couldn't open ${dirname}: $!\n"; > > > > local $direntry; > > while( $direntry=readdir(DIR) ) { > > if( $direntry!~/^\./ ) { > > local @state=stat($dirname.$direntry); > > print "$dirname$direntry $state[2]\n"; > > if( (($state[2] & ~7777)>>12) ==4 ) { # A directory - recurse > > scandir($dirname.$direntry."/"); > > } > > } > > } > > > > closedir DIR; > >} > > > 1. I know, I write perl like a C programmer, I can't help it. Feel free > to show me how it's done. > 2. For some strange reason, the moment I recurse once, the entire loop > structure exits. I suspect it's because the DIR handle is global. Will > any perl guru comment on how it's supposed to be done? > 3. What the @$([EMAIL PROTECTED] is the difference between "my" and "local"? Which > one should I use here? >
I am far from a perl guru, but I from some head banging on some similar stuff, try using my $dir; opendir ($dir, $dirname) or die "Couldn't open ${dirname}: $!\n"; .. close($dir); I do something similar with open in order to be able to return the handler, I believe opendir should function the same. IIRC DIR which is a clear word (or something similar as perl calls) doesn't behave exactly like a regular variable, or maybe because you don't define it as my it behaves as a global. > Thanks, > > Shachar > > -- > Shachar Shemesh > Lingnu OpenSource Consulting > http://www.lingnu.com/ > > > ================================================================= > To unsubscribe, send mail to [EMAIL PROTECTED] with > the word "unsubscribe" in the message body, e.g., run the command > echo unsubscribe | mail [EMAIL PROTECTED] > > > +++++++++++++++++++++++++++++++++++++++++++ > This Mail Was Scanned By Mail-seCure System > at the Tel-Aviv University CC. > ================================================================= To unsubscribe, send mail to [EMAIL PROTECTED] with the word "unsubscribe" in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED]