Danny Fang wrote:
Hi,

Hello,

I've a script below which reads the contents of a text file which
contains the output of the `ls -lR` in UNIX.

Why?? Why not just use File::Find?


This script reads the
list of html files contained in the text file, and for every file
it checks to see it contains some specified HTML tags or not. If
it does not, this script then inserts the specified tag (hardcoded
in this script) into the respective HTML file. ## test3b.pl
#!/usr/bin/perl/perl -w
$infile = shift(@ARGV);
open(INFILE, "$infile"); ## reads input file from command line - results of 'ls -lR'

You should *always* verify that the file opened correctly.


while(<INFILE>){
   next if(/total\d+/);    ## ignore total XXX in the output of ls -lR

That won't match because there is a space between 'total' and \d+.


next if(/$/);

You are saying skip this line if matches end-of-line but *every* line matches.


   if(/:$/){
      chomp;
      s/://;

Your match isn't anchored so if the dir name contains a colon it will be removed: './some:dir:' will become './somedir:'.


      $path = $_;   ## get path name of the file in ls -lR
   }
   else{
      @array = split(/\s+/, $_);
      $fileName = $array[8];

That will not work if the file name contains any whitespace characters.


$timeVal = $array[7];

You are not using this variable anywhere else which is why you get the warning 'Name "main::timeVal" used only once'. Also, $array[7] could contain the year instead of the time depending on how old the file is.


open(HTMLFILE, "$fileName");

You should *always* verify that the file opened correctly.


      @htmlContents = <HTMLFILE>;
      for($i; $i<@htmlContents; $i++){
         if($htmlContents[$i] !~ /<head>/){

You are testing *every* line in the file for the '<head>' tag. This will only work if every line does contain the tag. It also won't work if the file contains '<HEAD>' instead of '<head>'.


open(NOHEAD, ">NoHead.txt");

You are overwriting the file each time you open it so in the end it will only contain one line. Also, you should *always* verify that the file opened correctly.


print NOHEAD $path/$fileName; //compile a list of html files which do not have <head>

You are dividing the numeric value of $fileName by the numeric value of $path and then printing that value to the file? Comments in Perl begin with # not //.


            close(NOHEAD);
         }
         if($htmlContents[$i] !~ /^<\/script>history\.forward\(\)\b/){
            if($htmlContents[$i] =~ /<head>/){
               s/<\/head>/<\/script>history\.forward\(\)\n<\/head>/;
               open(REPLACEMENT, ">$path/$fileName");

Again, you are overwriting the file each time you open it so in the end it will only contain one line and you should *always* verify that the file opened correctly.


print REPLACEMENT "$htmlContents[$i]"; ## rewrite substituted value back to original file.
close(REPLACEMENT);
}
}
}
close(HTMLFILE);
}
}
close(INFILE);
However, I obtained the error below("Useless use of a variable in void
context at ./test3b.pl line 37") below, in which I'm do not how I
could resolve it:

Which line is number 37?



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to