Help - Search - Member List - Calendar
Full Version: PERL error message - Useless use of a variable in void context
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Danny Fang
Hi,

I've a script below which reads the contents of a text file which contains the output of the `ls -lR` in UNIX. 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'
while(<INFILE>){
next if(/totald+/); ## ignore total in the output of ls -lR
next if(/$/);
if(/:$/){
chomp;
s/://;
$path = $_; ## get path name of the file in ls -lR
}
else{
@array = split(/s+/, $_);
$fileName = $array[8];
$timeVal = $array[7];
open(HTMLFILE, "$fileName");
@htmlContents = <HTMLFILE>;
for($i; $i<@htmlContents; $i++){
if($htmlContents[$i] !~ /<head>/){
open(NOHEAD, ">NoHead.txt");
print NOHEAD $path/$fileName; //compile a list of html files which do not have <head>
close(NOHEAD);
}
if($htmlContents[$i] !~ /^</script>history.forward()b/){
if($htmlContents[$i] =~ /<head>/){
s/</head>/</script>history.forward()n</head>/;
open(REPLACEMENT, ">$path/$fileName");
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:

/opt/IBM/WebSphere/admin >>./test3b.pl /tmp/lender.txt
Useless use of a variable in void context at ./test3b.pl line 37.
Name "main::timeVal" used only once: possible typo at ./test3b.pl line 19.

Could anyone kindly help me out with this error?

Any suggestions or critique to this script is indeed welcomed.

Thanks



---------------------------------
Celebrate Yahoo!'s 10th Birthday!
Yahoo! Netrospective: 100 Moments of the Web

Peter Scott
In article <[Email Removed]>,
[Email Removed] (Danny Fang) writes:
QUOTE
--0-972884666=:30240
Content-Type: text/plain; charset=us-ascii

Hi,

I've a script below which reads the contents of a text file which contains the output of the `ls -lR` in UNIX. 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'
while(<INFILE>){
next if(/totald+/);    ## ignore total in the output of ls -lR
next if(/$/);
if(/:$/){
chomp;
s/://;
$path = $_;  ## get path name of the file in ls -lR
}
else{
@array = split(/s+/, $_);
$fileName = $array[8];
$timeVal = $array[7];
open(HTMLFILE, "$fileName");
@htmlContents = <HTMLFILE>;
for($i; $i<@htmlContents; $i++){
if($htmlContents[$i] !~ /<head>/){
open(NOHEAD, ">NoHead.txt");
print NOHEAD $path/$fileName;  //compile a list of html files which do not have <head
close(NOHEAD);
}
if($htmlContents[$i] !~ /^</script>history.forward()b/){
if($htmlContents[$i] =~ /<head>/){
s/</head>/</script>history.forward()n</head>/;
open(REPLACEMENT, ">$path/$fileName");
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:

/opt/IBM/WebSphere/admin >>./test3b.pl /tmp/lender.txt
Useless use of a variable in void context at ./test3b.pl line 37.
Name "main::timeVal" used only once: possible typo at ./test3b.pl line 19.

Could anyone kindly help me out with this error?

Did you really post the right code? Because that code has
basic syntax errors on line 21 where the programmer has
thought that // introduces a Perl comment, and also left
out quote marks, thereby doing a division by a string.
It should have (probably) generated the error

Bareword found where operator expected

The code does not use strict, which makes me disinclined
to read much more; if someone doesn't ask Perl to help
them get their program correct then they should expect
obscure problems.

I have no idea what line 6 was supposed to be, but it's
wrong.

The open statement is not checked for success and contains
a useless stringification.

The first clause of the for statement looks to me like
the source of the error you cite (it's a warning). The
programmer either didn't understand what a for statement
was or was very careless. Either way, there are so
many serious errors here that there are sufficient
grounds for rejecting the integrity of this program
and it should either be rewritten from requirements or
every line inspected with a magnifying glass.

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/

John W. Krahn
Danny Fang wrote:
QUOTE
Hi,

Hello,

QUOTE
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?


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


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

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


QUOTE
next if(/$/);

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


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


QUOTE
$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.


QUOTE
$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.


QUOTE
open(HTMLFILE, "$fileName");

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


QUOTE
@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>'.


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


QUOTE
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 //.


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


QUOTE
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


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.