Pipe to list all readable text files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Trevor17
    New Member
    • May 2009
    • 1

    Pipe to list all readable text files

    Hello All,

    I am currently in a Perl programming class and our assignment was:

    Create a filehandle with the open function that uses a pipe to list all the files in your current directory and will print only those files that are readable text files. Use the die function to quit if the open fails.

    After several hours, i have completed a program that does find all of the readable files in the current directory however I could not figure out how to do so using the pipe command. Here is what i have so far:

    #!/usr/bin/perl
    $directory="./";
    opendir(FH, $directory) || die "Not readable \n";
    @allfiles= readdir(FH);
    closedir(FH);

    foreach $file (@allfiles){
    unless (($file eq ".") || ($file eq "..")){
    print "$file \n" if -r $file;
    }
    }

    I have been working on this code for a while now and any help would be greatly appreciated. Thank you!
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    If the point is to use a pipe open then you won't be using perls -r file test operator but whatever the operating system the script runs on has to check if a file is readable.:

    Code:
    open(PIPE, "| operating system commands here") or die "$!";
    while(<PIPE>){
       print "$_\n";
    }

    Comment

    Working...