For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2007 > problem printing contents of file in directory









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author problem printing contents of file in directory
Alok Nath

2007-06-28, 3:59 am

Hi,
Can anybody tell me why its not printing the contents of each
inside a particular folder ?
I lost all my hairs scratching my head.
Thanks
Alok.

my $tstToRunDir = "C:\\perlScripts" ;
my $fileInTstToRunDir ;

opendir TST2RUN, $tstToRunDir || die "Failed to open $tstToRunDir $!\n" ;
open RESULTS_LOG, ">>ResultLog.log" || die "Failed to open log\n" ;

while ($fileInTstToRunDir = readdir (TST2RUN)){
chomp $fileInTstToRunDir ;
#open the file and get connection description and test description
open FILE_2RUN, $fileInTstToRunDir || die " Failed to open $fileInTstToRunDir:$!\n" ;
print "File is : $fileInTstToRunDir\n" ;
next if($fileInTstToRunDir =~ m/^./ ) ;

while( <FILE_2RUN> ){
print $_ ;
if ($_ =~ m/<Test Description/ ){
print $_ ;
}
}

print RESULTS_LOG "File is : $fileInTstToRunDir\n" ;
#$count++ ;
# close FILE_2RUN ;

}
close RESULTS_LOG ;
close TST2RUN ;



________________________________________
________________________________________
____
Sucker-punch spam with award-winning protection.
Try the free Yahoo! Mail Beta.
http://advision.webevents.yahoo.com...tures_spam.html
John W. Krahn

2007-06-28, 3:59 am

alok nath wrote:
> Hi,


Hello,

> Can anybody tell me why its not printing the contents of each
> inside a particular folder ?


Yes I can, and so can Perl's documentation:

perldoc -f readdir


> my $tstToRunDir = "C:\\perlScripts" ;
> my $fileInTstToRunDir ;
>
> opendir TST2RUN, $tstToRunDir || die "Failed to open $tstToRunDir $!\n" ;
> open RESULTS_LOG, ">>ResultLog.log" || die "Failed to open log\n" ;


The logical or operator '||' has relatively high precedence so neither opendir
nor open will die if an error is encountered. You need to either use parentheses:

opendir( TST2RUN, $tstToRunDir ) || die "Failed to open $tstToRunDir $!\n" ;
open( RESULTS_LOG, ">>ResultLog.log" ) || die "Failed to open log\n" ;

or use the low precedence 'or' operator:

opendir TST2RUN, $tstToRunDir or die "Failed to open $tstToRunDir $!\n" ;
open RESULTS_LOG, ">>ResultLog.log" or die "Failed to open log\n" ;


> while ($fileInTstToRunDir = readdir (TST2RUN)){
> chomp $fileInTstToRunDir ;


$fileInTstToRunDir comes directly from the file system so there is no reason
to use chomp.


> #open the file and get connection description and test description
> open FILE_2RUN, $fileInTstToRunDir || die " Failed to open $fileInTstToRunDir:$!\n" ;


Again, there is a precedence problem with the '||' operator. The file name in
$fileInTstToRunDir was obtained from the $tstToRunDir directory so you need to
include the directory name in order to access it:

open FILE_2RUN, "$tstToRunDir/$fileInTstToRunDir"
or die " Failed to open $tstToRunDir/$fileInTstToRunDir:$!\n" ;


> print "File is : $fileInTstToRunDir\n" ;
> next if($fileInTstToRunDir =~ m/^./ ) ;
>
> while( <FILE_2RUN> ){
> print $_ ;
> if ($_ =~ m/<Test Description/ ){
> print $_ ;
> }
> }
>
> print RESULTS_LOG "File is : $fileInTstToRunDir\n" ;
> #$count++ ;
> # close FILE_2RUN ;
>
> }
> close RESULTS_LOG ;
> close TST2RUN ;



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com