ZWcarp has asked for the wisdom of the Perl Monks concerning the following question:
#!/bin/bash # $1 results directory # to run, e.g., # step1 /your/path/to/results # need one argument ! if [ $# == 1 ]; then # scripts directory d=$( dirname $( readlink -m $0 ) ) #results directory res=$( readlink -m $1 ) # loop through the cases in data for sample in $( ls ${res}/ );do echo $sample; done; else echo "error - need to supply an argument" fi
What would the best way to write this in perl be ? Is there anyway to avoid using opendir, readdir, and closedir? and to avoid having to use CWD to get the absolute path ( as readlink -m . does in bash, or a way to write the for loop without having to first read into an array?
. The following perl script does the same thing, but I want to streamline it, even if that means really obfuscating it, any suggestions?#!/usr/bin/perl -w use strict; use Cwd 'abs_path'; use Cwd; use File::Basename; if (defined ($ARGV[0])){ #scripts directory my $d= getcwd();; #results directory my $res= abs_path($ARGV[0]); # loop through the cases in data opendir( DIR, $res ) || die "Error in opening dir $res\n"; my @files = readdir(DIR); close(DIR); foreach my $sample (@files){ print $sample . "\n"; } } else {print "Need to supply argument\n"};
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Most effective bash to perl for directory listing
by BrowserUk (Patriarch) on Jul 18, 2012 at 18:02 UTC | |
by ZWcarp (Beadle) on Jul 18, 2012 at 18:29 UTC | |
by BrowserUk (Patriarch) on Jul 18, 2012 at 19:07 UTC | |
by aaron_baugher (Curate) on Jul 18, 2012 at 22:00 UTC |