Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Most effective bash to perl for directory listing

by ZWcarp (Beadle)
on Jul 18, 2012 at 17:19 UTC ( #982492=perlquestion: print w/ replies, xml ) Need Help??
ZWcarp has asked for the wisdom of the Perl Monks concerning the following question:

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"};

Comment on Most effective bash to perl for directory listing
Select or Download Code
Re: Most effective bash to perl for directory listing
by BrowserUk (Pope) on Jul 18, 2012 at 18:02 UTC

    Why does the script get the current directory? It never does anything with it.

    The best I can tell from looking at the perlish version, it boils down to:

    perl -E" say for glob qq[$ARGV[0]/*] "

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Wow that is awesome...Sorry.. I removed a lot of stuff from the scripts to make it simpler for the post, in mine it uses the current directory. Is there a way to get glob to output just the directory names but not their entire paths (without just regexing it)---> This is the current output of what you posted

      ../results//jobs ../results//logs ../results//step0_tophat ../results//step1_2cuffcompare ../results//step1_cufflinks ../results//step2_cuffmerge ../results//step3_cuffdiff ../results//step4_createTranscriptome ../results//step6_bowtie_pe ../results//step7_bowtie_se ../results//step8_bowtie_hg19 ../results//step9_tophat_unmapped_hg19
      This is what I was looking for, if it is possible:
      jobs logs step0_tophat step1_2cuffcompare step1_cufflinks step2_cuffmerge step3_cuffdiff step4_createTranscriptome step6_bowtie_pe step7_bowtie_se step8_bowtie_hg19 step9_tophat_unmapped_hg19
      Thanks so much for your time!

      On a side note, I really want to learn more shorthand tricks like this, but they are really hard to find without asking a direct question... are you aware of any resources that instruct specifically on useful shorthand alternatives .. for example the difference between my script and your response.

        Is there a way to get glob to output just the directory names but not their entire paths (without just regexing it)

        No. But regexing it isn't hard:

        perl -E"say for map{ s[$ARGV[0]/][]; $_} glob qq[$ARGV[0]/*]"
        are you aware of any resources that instruct specifically on useful shorthand alternatives

        Erm. There was a thread a few years called something like Cheap idioms that had some.

        Typing "idioms" into the search box top left brings up some similar threads.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

        Is there a way to get glob to output just the directory names but not their entire paths?

        Yes. glob returns pathnames matching the pattern you give it. So if you give it ../results/*, it'll return the filenames in that directory in that format beginning with ../results/, just like the shell command find will do. So if you don't want the rest of the path, chdir into the directory first and then use glob just on the filenames:

        chdir $ARGV[0] or die $!; print "$_\n" for glob '*';

        Aaron B.
        Available for small or large Perl jobs; see my home node.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://982492]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-09-18 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (108 votes), past polls