So I decided to learn Perl yesterday, and I'm writing a Perl script to try and parse HTML files (not using regexps for the actual parsing).
Anyway; I have this subroutine to iterate over each argument in @ARGV and, if one of them is an existing file, return it's filename (and path if it's not in the cwd; the path would have to be given on the command line). If it breaks out of the loop and still hasn't found a file, it returns the text "no file found". For some reason, although I'm calling it correctly and although it's actually telling me it found the file; it still returns "no file found":
sub getHTMLfile {
foreach $arg (@ARGV) {
if (-e $arg) {
print "Found file \"$arg\"\n";
return $arg;
}
}
return "no file found";
}
This is where I call getHTMLfile:
sub __main {
$htmlFile = getHTMLfile(); # Get file to parse
if ($htmlFile == 'no file found') { # getHTMLfile() didn't find a suitable
# file in the argument list.
print "$0: no existing file available for parsing\n";
exit;
}
$htmlBuffer = readFile($htmlfile); # Read it's contents into memory
if ($htmlBuffer == 'no data found') {
print "$0: unable to find data in the file, is it blank?\n";
exit;
}
pre_parse($htmlBuffer); # Parse syntax errors and stuff
}
I'm getting the "$0: no existing file available for parsing" message when I call the script with this (bash):
$ perl parsehtml.pl htmlfile.html
Found file "htmlfile.html"
parsehtml.pl: no existing file available for parsing
htmlfile is a simple, er, HTML file I put together:
<html>
<head>
<title> This is the title </title>
</head>
<body> <!-- This is a comment and will be ignored-->
Here is some body text <br />and this is on the second line
and so is this
</body>
</html>
Thanks.