new to perl need help
sanjaysingh
created: 2006-03-28 22:36:58
i am trying to read a DIR and then read the content of each file in that DIR. Kindly help it.
#!/usr/local/bin/perl

 opendir DH, "Sanjay" or die "fail" ;

while($temp = readdir(DH))  {
     print "$temp\n";
 open FILE, "$temp" or die "fail";
 print "pass";
 while(<$temp>) {
 print "sucess";
 }
 }

Re: new to perl need help
created: 2006-03-28 22:47:37
The names that come back from readdir are the basename only. You'll have to prepend the directory name to get a good path. Or just use glob, as it's far easier for the beginner.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Re: new to perl need help
created: 2006-03-28 23:25:18
Here is probably what you are looking for.
my $CurrentDir="."; # Or "Sanjay";
opendir DH, "." or die "failed to readdir $CurrentDir: $!";

while($temp = readdir(DH))  {
     print "$temp\n";
   if ($temp eq "." or $temp eq ".." or -d $temp){
      next; # Do not open . or ..
   }
   open FILE, "$CurrentDir/$temp" or die "failed to open file $temp:$!\n";
   print "pass";
   while(<$temp>) {
         print "sucess: $_";
   }
}
Normally - I would not code this way - this is written to help you get past initial roadblocks.

     "For every complex problem, there is a simple answer ... and it is wrong." --H.L. Mencken

Re: new to perl need help
created: 2006-03-28 23:49:53
#!/usr/local/bin/perl
use strict;
opendir(DIR, ".");
my @files = grep/^\w+/, readdir(DIR);
for (@files) {
 undef $/;
 open(IN, "$_") || die "Cannot open $_";
 my $str = ;
 print $str;
 close(IN);
}

If you need to read only text file you can grep only .txt file. For eg: my @files = grep/.txt$/, readdir(DIR);

Re: How do I read the contents of each file in a directory?
mk.
created: 2006-03-29 09:08:37
is this kind of what you wanted?! no sure i got it right... =/

perl -pe '' Sanjay/*


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@

perlmonks.org content © perlmonks.org and gube, merlyn, mk., NetWallah, sanjaysingh

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03