Connecting Tech Pros Worldwide Help | Site Map

Pipe to list all readable text files

Newbie
 
Join Date: May 2009
Posts: 1
#1: May 19 '09
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's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: May 20 '09

re: Pipe to list all readable text files


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.:

Expand|Select|Wrap|Line Numbers
  1. open(PIPE, "| operating system commands here") or die "$!";
  2. while(<PIPE>){
  3.    print "$_\n";
  4. }
  5.  
Reply