Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Require input reading files recursively in perl

by CountOrlok (Friar)
on Apr 17, 2013 at 13:22 UTC ( #1029146=note: print w/ replies, xml ) Need Help??


in reply to Require input reading files recursively in perl

General disclaimer: use strict/warnings.

Using a global filehandle (or in general any global variable that can be modified) with recursion is a *bad* idea. Instead use lexcials:
opendir(my $SCR , $dir) or die "Can't open $dir: $!"; while( defined (my $file = readdir $SCR) )
Don't forget to closedir when done.


Comment on Re: Require input reading files recursively in perl
Download Code
Replies are listed 'Best First'.
Re^2: Require input reading files recursively in perl
by jesuashok (Curate) on Apr 17, 2013 at 13:28 UTC
    Thanks a lot. Below is the newer version and it works fine. Appreciate the time and your valuable input.
    #!/usr/bin/perl use strict; use warnings; my $parent_dir = "/panther/home/efxprod/support"; process_dir($parent_dir); sub process_dir { my $dir = shift; print "Processing $dir\n"; opendir(my $SCR , $dir) or die "Can't open $dir: $!"; while( defined (my $file = readdir $SCR) ) { next if ($file =~ /\.$/ ); next if ($file =~ /\.txt$/ ); if ( $file =~ /\.sh$/ ) { print "not a regullar file: $file\n"; } elsif ( $file =~ /\.pl$/) { print "perl $file\n"; } elsif ( -d "$dir/$file" ) { print "directory : $dir/$file\n"; process_dir("$dir/$file/"); #next; #} elsif ( $file } else { print "Else :$file\n" if ( -B "$dir/$file"); } print "file -> $file\n"; } closedir($SCR); }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2015-07-30 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls