Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bareword found err, using activestate on win98, full code pasted 1

Status
Not open for further replies.

vasah20

Programmer
Feb 16, 2001
559
0
0
US
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?

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(&quot;Cannot close $file: \n&quot;);
    $/ = $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 &quot;open(SLFILE,$file) || die(&quot;Cannot&quot;
(Might be a runaway multi-line &quot;&quot; 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 &quot;description&quot; myKWFile &quot;C:\\ATA\\website\\&quot;
 This will replace all the <meta name=&quot;description&quot; content=&quot;xxx&quot;>
 tags within the &quot;C:\\ATA\\website&quot; directory with
 <meta name=&quot;description&quot; content=&quot;data_from_myKWFile&quot;>

 Currently parses only .htm or .html files.
EOF
;
die(&quot;Not enough parameters supplied! Aborting execution &quot;);
} 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 (&quot;Could not open $ARGV[2]! $!);
            while(<FILES>)
                $mydir = &mydir . $_ . &quot;,&quot;;
            close(FILES);
        } else {    #a whole directory was specified
            $mydir = $ARGV[2];
        }
    }
    else {
        $mydir = &quot;.&quot;;       #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(&quot;Cannot slurp $file: \n&quot;);
    $/ = undef;         
    my $mySlurp = <SLFILE>;
    close(SLFILE) || die(&quot;Cannot close $file: \n&quot;);
    $/ = $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 &quot;Cannot opendir $mydir: $!&quot;;
    @dirarr = readdir(DIR);
    closedir(DIR);
} else {
    @dirArr = split($mydir,&quot;,&quot;);
    $mydir = &quot;&quot;;
}
#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=\&quot;$lookFor\&quot;s+?content=\&quot;)(.*?)(\&quot;)/$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\=\&quot;$lookFor\&quot; contents\=\&quot;$desc\&quot;><\/head>/i;
        push(@filesModArr,$item);
    }

    #now, we open the file again, this time for writing
    open(FILEOUT, '>', &quot;$mydir$item&quot;) || die(&quot;Can't open $item for writing! \n&quot;);
    print(FILEOUT $slurp)|| die (&quot;Can't write to FILEOUT! $!\n&quot;);
    close(FILEOUT) || die(&quot;Can't close FILEOUT! $!\n&quot;);
}
###########end main processing

###########record keeping stuff
open(LOG,&quot;> $datafile.log&quot;) || die(&quot;Can't open $datafile.log for logging: $!\n&quot;);
print(LOG &quot;Perl META tag replacer by Leo Mendoza\nRun at $lt\n\n&quot;);
print(LOG &quot;Using datafile $datafile\nDescription: $desc\n\nFiles modified:\n&quot;);
my $fileList = join(&quot;\n&quot;,@filesModArr) . &quot;\n&quot;;
print(LOG $fileList);
$lt = localtime;
print(LOG &quot;Execution stopped at $lt&quot;);
close(LOG) || die(&quot;Can't close $datafile.log: $!\n&quot;);
print(&quot;Succesfully ended @ $lt.\nSee $datafile.log for more details.\n&quot;);

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
 
Your problem may have something to do with your print <<TAGNAME lines. They don't have a semicolon on the end of them, so I think perl thinks your tag runs until the next semicolon. They should look like this:
Code:
print <<TAGNAME;
this is stuff to print
so is this
this too
TAGNAME
DO NOT put a semicolon on the line after the closing tag (as you have done). It is safest to put a BLANK line after the closing tag.

Also on this line:
Code:
40  open(FILES,$ARGV[2]) || die (&quot;Could not open $ARGV[2]! $!);
You are missing a closing quote, and that would definitely throw things off.

on these lines:
Code:
41       while(<FILES>)
42           $mydir = &mydir . $_ . &quot;,&quot;;
You are missing the brackets around the while code block. PERL ALWAYS requires brackets around blocks!

And on this line:
Code:
74 my @filesModArr,@metas,@htmArr,@dirArr;
You need to put parens around the list of variables you are defining.

Fix those and report back to us. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Also on lines 78 and 86 you're using @dirarr instead of @dirArr (watch the case of your variables). I fixed all the above and this and it compiles with no errors. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Thanks Tracy, you deserve a star!

Everything you stated fixed it, so now it compiles. Have yet to test functionality, but that'll be a new thread.

See - this is what I get for coding in VBA for a week and a half... I get real sloppy with my code...

Thanks again. leo

------------
Leo Mendoza
lmendoza@students.depaul.edu
 
It might help to get a text editor like EditPlus ( that color codes the syntax for you. That's how I found the missing quote error. I also have it set up to run a syntax check and put the results in an output window, where all I have to do is double-click on an error message to find the line with the error. I fix it, click on my syntax check button, and it saves the changes and reruns the perl interpretor. Makes it very easy and quick to find errors. If you decide to use EditPlus, I can tell you how to set up the syntax-check toolbar button. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Could I suggest that you put

use strict;

at the top of each script longer than about 3 lines, it will pick up variable mispellings etc. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Mike, he does have a use strict in there. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
so he does.... sorry, I just missed it. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
I'd appreciate the syntax-check toolbar button.
I'm using Multi-Edit right now and it's a great editor, though after a short eval the EditPlus
seems much easier to use/configure without any lack of functionality. And I don't have
the syntax-check in Multi-Edit... Jean Spector
QA Engineer @ mSAFE Ltd.
 
Tracy,

Could you post instructions on how to set up the Syntax check for EditPlus?

Thanks!
 
It's really pretty easy to do, here's how:

Click on &quot;Tools/Configure User Tools&quot;.

Click on the &quot;Add Tool>>&quot; button and select &quot;Program&quot;.

Fill in these fields:

Menu Text: PERL Syntax Check
Command: c:\perl\bin\perl
Argument: -W -c $(FileName)
Initial Directory: $(FileDir)

Check the &quot;Capture Output&quot; box

Save it.

Make sure you have the User Tools buttons (the little hammer buttons) displayed.

Open up your perl program, and click on the new button.

The output window will open at the bottom of the window with your output in it. You can double-click on an error and you will jump right to that line.

Additional Notes:

The &quot;Command&quot; above works for the default installation of ActivePerl. If your perl is in a different place, enter the path to your perl.exe. If you have the path to perl.exe in your PATH environment variable, you can just enter &quot;perl&quot;.

If you have libraries stored in other directories, you can add -lc:\path\to\libs to the arguments.

To make a button that RUNS the program, do the same thing, except leave the -c out of the arguments.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top