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.

Edited 6 Years Ago by chrisname: n/a

Your question intrigued me, so I copied your code and got the same error. Your problem is with this if ($htmlFile == 'no file found') .

Instead of the == operator, use the eq operator.

Your actual problem, however, is that this will only work for the first HTML file in @ARGV. return $arg will exit the function as soon as it finds a file. If you want it to find multiple files, push $arg onto an array and return the entire array

Your question intrigued me, so I copied your code and got the same error. Your problem is with this if ($htmlFile == 'no file found') .

Instead of the == operator, use the eq operator.

Ok :)

Your actual problem, however, is that this will only work for the first HTML file in @ARGV. return $arg will exit the function as soon as it finds a file. If you want it to find multiple files, push $arg onto an array and return the entire array

That was intentional. It's meant to return as soon as it finds an existing file.

I don't have bash but when I tested your Perl script from the Windows command line (after copying an html file into the current directory it worked -- until it died because I don't have a readFile subroutine. But the point is that I didn't get the "$0: no existing file available for parsing" message on my platform. It's a mystery.

Remember that naming variables in Perl is case-sensitive. I see you have variables named "$htmlFile" and "$htmlfile" in your program, but I don't see how that would cause this particular error.

I don't have bash but when I tested your Perl script from the Windows command line (after copying an html file into the current directory it worked -- until it died because I don't have a readFile subroutine. But the point is that I didn't get the "$0: no existing file available for parsing" message on my platform. It's a mystery.

Remember that naming variables in Perl is case-sensitive. I see you have variables named "$htmlFile" and "$htmlfile" in your program, but I don't see how that would cause this particular error.

Thanks for pointing that out, I didn't notice that! No wonder I can't read the file :)

You're welcome. I neglected to say the test worked for me only after I changed the '==' operator to 'eq' as ItecKid pointed out. Also, I recommend using the strict module in any Perl script -- just add use strict; -- unless you have a particular reason not to use it. It warns you of unused or misspelled variable names, etc.

You're welcome. I neglected to say the test worked for me only after I changed the '==' operator to 'eq' as ItecKid pointed out. Also, I recommend using the strict module in any Perl script -- just add use strict; -- unless you have a particular reason not to use it. It warns you of unused or misspelled variable names, etc.

Ok, cool, thanks. I'd seen that before but didn't know what it does.

Thank you.

Also use strict; nags you until you declare all variables (by adding 'my' in foreach [B]my[/B] $arg (@ARGV) for example) which seems like a nuisance at first but you soon get used to it.

Also use strict; nags you until you declare all variables (by adding 'my' in foreach [B]my[/B] $arg (@ARGV) for example) which seems like a nuisance at first but you soon get used to it.

Oh, I was declaring variables before using them:

use strict;

my @days = ("Monday", "Tuesday", "Wednesay",
            "Thursday", "Friday", "Saturday", "Sunday"
           );
           
my $day = "";
           
foreach $day (@days) {
    print "$day\n";
}

when I could have done this

use strict;

my @days = ("Monday", "Tuesday", "Wednesay",
            "Thursday", "Friday", "Saturday", "Sunday"
           );
           
foreach my $day (@days) {
    print "$day\n";
}

>

One more thing...
I forgot to mention that you'll see a lot of examples where the first line is something like #!/usr/local/bin/perl -w where the -w turns on warnings. Warnings can tell you about unused filehandles or variables with no assigned value. Or you can turn on warnings by a use warnings; statement in your program, or perl -w from the command line.

One more thing...
I forgot to mention that you'll see a lot of examples where the first line is something like #!/usr/local/bin/perl -w where the -w turns on warnings. Warnings can tell you about unused filehandles or variables with no assigned value. Or you can turn on warnings by a use warnings; statement in your program, or perl -w from the command line.

Oh, thanks.

This question has already been answered. Start a new discussion instead.