CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [eyebrowsbutt] Reading files from directory and subdirectory: Edit Log



Zhris
Enthusiast

Sep 11, 2012, 2:34 PM


Views: 1450
Re: [eyebrowsbutt] Reading files from directory and subdirectory

Hey,

If you use a directory that doesn't exist then your code will exit with the "Cannot open directory" error. If all.fna is a directory that sits in the same directory as your Perl script, then you are fine. If you run with just the exit removed, is the output better than before.

I re-wrote your recursive subroutine, and simplified the GC subroutine to just print the file_path. It might be a good starting point, since it only prints each file it recursively comes across. Printing too many unformatted strings can become confusing. If it works out, replace the GC subroutine with yours (don't forget to remove the final exit):


Code
#!/usr/bin/perl 
use warnings;
use strict;

list_recursively('all.fna');
exit;

sub list_recursively
{
my ($dir_path) = @_;

opendir my $dh, $dir_path or die "cannot open dir $dir_path: $!";

foreach my $path ( map { "$dir_path/$_" } grep { !/^\.\.?$/ } readdir $dh )
{
if (-f $path)
{
GC($path);
}
elsif (-d $path)
{
list_recursively($path);
}
}

closedir $dh;

return;
}

sub GC
{
my ($file_path) = @_;

print "$file_path\n";

return;
}


Chris


(This post was edited by Zhris on Sep 11, 2012, 3:19 PM)


Edit Log:
Post edited by Zhris (Enthusiast) on Sep 11, 2012, 2:36 PM
Post edited by Zhris (Enthusiast) on Sep 11, 2012, 2:49 PM
Post edited by Zhris (Enthusiast) on Sep 11, 2012, 3:19 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives