in reply to Whenever perl meets namedpipe it gets blocked
You can do this using either BFS or DFS. Recursive method call is a great way to solve this kind of problems.
#!/usr/bin/perl -w use strict; sub bfs { my $dir = shift; return unless defined $dir; opendir(my $dh, $dir) || die; while (readdir $dh) { next if /^\.{1,2}$/; $_ = "$dir/$_"; if (-d) { push @_, $_; ### } elsif (-f) { print "$_\n"; } else { print "Something else: $_\n"; } } closedir $dh; bfs(@_); } sub dfs { my $dir = shift; return unless defined $dir; opendir(my $dh, $dir) || die; while (readdir $dh) { next if /^\.{1,2}$/; $_ = "$dir/$_"; if (-d) { unshift @_, $_; ### } elsif (-f) { print "$_\n"; } else { print "Something else: $_\n"; } } closedir $dh; dfs(@_); } print "=== BFS ===\n"; bfs(@ARGV); print "=== DFS ===\n"; dfs(@ARGV);
|
|---|