in reply to tree command
You're iterating over the list of files twice, once in the grep & once in the foreach, and you don't need to do that. Also glob is easier to use than opendir/readdir, IMHO. So you could have done something like this
use v5.20; use warnings; use autodie qw/:all/; use File::Glob qw/:bsd_glob/; use File::Spec; use File::Basename; sub showtree { my ($dir,$indent) = @_; $indent .= ' '; my @list = bsd_glob(File::Spec->catfile($dir,'*')); foreach my $f (@list) { if (-d $f) { say $indent,'+-',basename($f); showtree($f,$indent . '|'); } if (-l $f) { say $indent,'+-',basename($f),' --> ',readlink($f); } } } showtree($ARGV[0] // '.');
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: tree command
by Skeeve (Parson) on Oct 13, 2015 at 11:43 UTC | |
by RichardK (Parson) on Oct 13, 2015 at 14:56 UTC | |
by Skeeve (Parson) on Oct 14, 2015 at 06:21 UTC | |
by choroba (Cardinal) on Oct 14, 2015 at 09:45 UTC | |
by Skeeve (Parson) on Oct 14, 2015 at 11:31 UTC | |
| |
by RichardK (Parson) on Oct 14, 2015 at 09:17 UTC | |
by Skeeve (Parson) on Oct 14, 2015 at 15:45 UTC |
In Section
Cool Uses for Perl