Hello all, I am wondering what the best way to write this bash script in perl would be. All the script is doing is getting the current directory, getting a different directory, and then finally printing out all the directories present in that different directory.
#!/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"};

In reply to Most effective bash to perl for directory listing by ZWcarp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.