in reply to Re: Simplify parsing a file in thread Simplify parsing a file
instead of using <> to run through the files, you could do something like:
That's excellent. I'll give that a shot. But can you tell me why the camel book says to do it this way if it doesn't really work?
For the regex part, you're right that that would work for standard HTML, but I have to parse JSP pages too, and they are allowed to nest tags like that. I'll try your while loop and see how that works, though.
As for the sadness of monks, I apologize. The reason I did it this way was to familiarize myself with syntax, standard perl functions, and little gotchas that are inherent in any language. Once I feel proficient with this skill, I'll be happy to keep you guys smiling :)
Re^3: Simplify parsing a file
by saintly (Scribe) on Apr 02, 2007 at 18:57 UTC
|
while( <> ) {
}
Will 'automagically' run through every line of each of the files the user specified on the command line. It is the equivalent of:
foreach $ARGV (@ARGV) {
open( THISFILE, $ARGV ) || next;
while( $_ = <THISFILE> ) {
}
}
Which is why your script was executing many times per file. UNIX programs traditionally don't automatically create new files from their input, they just slurp in all the input files they're given and then print output directly to STDOUT so it can be redirected. Consider:
$ grep 'foo' thisfile.txt thisOtherFile.txt
Which will run through all the files it's given and print matching lines. Perl allows the programmer to do this kind of task easily with the <> syntax. | [reply] [d/l] [select] |
|