Hey all -
I'm having a problem with this slice of code... I keep getting a bareword found error where I open the file. I've tried running this on my linux machine, and I don't get any problems. Any ideas?
I get other bareword found errors whenever I open a file anywhere else in the script...
here's the exact error:
Bareword found where operator expected at dir.pl line 62, near "open(SLFILE,$file) || die("Cannot"
(Might be a runaway multi-line "" string starting on line 51)
I know it'll extend the length of this post, but here's the full code if you want to look at it.
I'm using activestate 5.6.31 on win98.
Sorry bout the length of the post... I know it's better to have too much information than too little though.
TIA
leo leo
------------
Leo Mendoza
lmendoza@students.depaul.edu
I'm having a problem with this slice of code... I keep getting a bareword found error where I open the file. I've tried running this on my linux machine, and I don't get any problems. Any ideas?
Code:
############begin slurpFile
#param: takes in any filename
#slurp any file in, return the slurped string
sub slurpFile($) {
my $file = @_;
my $oldSlash = $/;
open(SLFILE,$file) || die("Cannot slurp $file: \n");
$/ = undef;
my $mySlurp = <SLFILE>;
close(SLFILE) || die("Cannot close $file: \n");
$/ = $oldSlash;
return($mySlurp);
}
###########end slurpFile
I get other bareword found errors whenever I open a file anywhere else in the script...
here's the exact error:
Bareword found where operator expected at dir.pl line 62, near "open(SLFILE,$file) || die("Cannot"
(Might be a runaway multi-line "" string starting on line 51)
I know it'll extend the length of this post, but here's the full code if you want to look at it.
Code:
#Created by Leo Mendoza, 2001.
#Used to help expedite html file processing.
use strict;
my $lt = localtime;
my ($item,$datafile,$mydir,$lookFor);
if ($#ARGV < 1) {
print <<EOF
Created by Leo Mendoza, 2001.
Usage: perl dir.pl replace datafile [directory]
Options:
replace can be any meta tag name (eg:description, keywords, author, etc...)
datafile is a CSV file for the content of the tag.
If directory is not supplied, the current directory is used.
ex: perl "description" myKWFile "C:\\ATA\\website\\"
This will replace all the <meta name="description" content="xxx">
tags within the "C:\\ATA\\website" directory with
<meta name="description" content="data_from_myKWFile">
Currently parses only .htm or .html files.
EOF
;
die("Not enough parameters supplied! Aborting execution ");
} else {
print<<EOL
perl <META> tag replacer by Leo Mendoza.
Executed at $lt
Parsing for: $ARGV[0]
Using datafile:$ARGV[1]
Searching directory: $ARGV[2]
...
EOL
;
$lookFor = $ARGV[0];
$datafile = $ARGV[1];
if (defined($ARGV[2])) {
if (-e $ARGV[2]) {
open(FILES,$ARGV[2]) || die ("Could not open $ARGV[2]! $!);
while(<FILES>)
$mydir = &mydir . $_ . ",";
close(FILES);
} else { #a whole directory was specified
$mydir = $ARGV[2];
}
}
else {
$mydir = "."; #default the directory to .
}
}
############begin slurpFile
#param: takes in any filename
#slurp any file in, return the slurped string
sub slurpFile($) {
my $file = @_;
my $oldSlash = $/;
open(SLFILE,$file) || die("Cannot slurp $file: \n");
$/ = undef;
my $mySlurp = <SLFILE>;
close(SLFILE) || die("Cannot close $file: \n");
$/ = $oldSlash;
return($mySlurp);
}
###########end slurpFile
###########begin main processing -> not in a sub, because I'm lazy :)
my $desc = slurpFile($datafile); #slurp the full data description
chomp($desc);
#GRAB ALL THE FILES IN THE DIRECTORY,STORE INTO THE ARRAY
my @filesModArr,@metas,@htmArr,@dirArr;
my $count=0;
if (-d $mydir) {
opendir(DIR, $mydir) || die "Cannot opendir $mydir: $!";
@dirarr = readdir(DIR);
closedir(DIR);
} else {
@dirArr = split($mydir,",");
$mydir = "";
}
#END FILE GRAB
foreach $item (@dirarr) {
if ($item =~ /(.*\.?)*(\.htm|\.html)$/i) { #look for files ending in 'htm | html'
push(@htmArr,$item);
}
}
#DO THE ARRAY PROCESSING HERE
foreach $item (@htmArr) {
my $slurp = slurpFile($mydir . $item); #get the full file
#grab every meta tag in the file
while ($slurp=~s/(\<meta[^\<]*\>)/-=-=$count=-=-/si) {
push (@metas,$1);
$count = scalar @metas;
}
#check to make sure some meta tags were found
if (scalar @metas > 0) {
for (@metas) {
s/(name=\"$lookFor\"s+?content=\")(.*?)(\")/$1$desc$3/si;
push(@filesModArr,$item);
}
#replace the meta tags in the code.
for ($_=0;$_<=$#metas;$_++) {
$slurp=~s/-=-=$_=-=-/$metas[$_]/si;
}
} else {
chomp($desc);
$slurp =~ s/\<\/head\>/<meta name\=\"$lookFor\" contents\=\"$desc\"><\/head>/i;
push(@filesModArr,$item);
}
#now, we open the file again, this time for writing
open(FILEOUT, '>', "$mydir$item") || die("Can't open $item for writing! \n");
print(FILEOUT $slurp)|| die ("Can't write to FILEOUT! $!\n");
close(FILEOUT) || die("Can't close FILEOUT! $!\n");
}
###########end main processing
###########record keeping stuff
open(LOG,"> $datafile.log") || die("Can't open $datafile.log for logging: $!\n");
print(LOG "Perl META tag replacer by Leo Mendoza\nRun at $lt\n\n");
print(LOG "Using datafile $datafile\nDescription: $desc\n\nFiles modified:\n");
my $fileList = join("\n",@filesModArr) . "\n";
print(LOG $fileList);
$lt = localtime;
print(LOG "Execution stopped at $lt");
close(LOG) || die("Can't close $datafile.log: $!\n");
print("Succesfully ended @ $lt.\nSee $datafile.log for more details.\n");
I'm using activestate 5.6.31 on win98.
Sorry bout the length of the post... I know it's better to have too much information than too little though.
TIA
leo leo
------------
Leo Mendoza
lmendoza@students.depaul.edu