in reply to Converting HTML tags to upper case
Silly question, perhaps, but why are you doing this?
Is it just an exercise to improve your coding skills? You probably already know that HTML tags are case insensitive.
Just curious.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Converting HTML tags to upper case
by Bern (Initiate) on Dec 12, 2006 at 12:54 UTC | |
# 1 - Prompt the user to enter the filename . #2 - Verify the file extension to make sure it's htm or html. #3 - Open the file and read each line of the file. #4 - Change all tags to uppercase. #5 - Tag attributes need to remain in lowercase. #6 - Original file needs renaming with the .old extension and the processed file needs the original filename. This is what I've got so far (not a great deal of progress). #1
#2 (I have the following regular expression) ($file =~ s/html|htm$/i). Would I be best putting this in an IF statement ?. #3 & #4
Struggling on part #5 !. #6 Got the feeling I'm going to need to use something like the following.
| [reply] [d/l] [select] |
by wfsp (Abbot) on Dec 12, 2006 at 13:50 UTC | |
It uses HTML::TokeParser::Simple as suggested by marto above. If there is HTML in the air I won't leave home without it. :-)
| [reply] [d/l] |
by talexb (Chancellor) on Dec 12, 2006 at 14:35 UTC | |
Some quick comments .. There's a lot to learn, so search for articles here on Perlmonks and do lots of reading. 99% of the time, the thing you want to do has already been thought of and coded up. It's amusing to reinvent the wheel, but only do so if you have plenty of time to learn. | [reply] [d/l] [select] |
by ww (Archbishop) on Dec 12, 2006 at 14:35 UTC | |
Re #2 - One good way to spoeed your learning is to run such constructs through a "try-out." Here's a fairly simply way to do so (using one specific bit of your proposed code):
(What's happening above is that we're stuffing a variety of possible names into an array, which makes for an easy, compact way to check them all.) and then tell perl to run a check ( -c ): >perl -c bern.pl Now, Perl has told you there's something wrong with that scheme (Hint: You've said you're checking to make sure the file has an .htm or .html extension. So why are you using substitution ( s/// )? Perhaps that's an "aha" moment. We don't want to substitute in the test (and yes, we do need that if in order to test without being sensitive to case). So what happens if we edit the script to test for a MATCH instead of attempting substitution?
Now, it passes the check... and running the script does this: >perl bern.pl Well, ugly, but "yep" -- all four bits of test data matched either htm or html. So we're done, right? BRRRRRRRRAATTTTTT! Let us suppose some evil user (and if you don't think "evil user" is redundant, beware!) tried to foist a file like "oneHTML.xyz" on you? Well, try it! OK, so now we know the test works in part, but not well enough to do what you want -- that is, not well enough to restrict the acceptable files to those with an extension of .htm, .html, .HTM or .HTML (though your trailing "i" does provide the case insensitivity you probably want in the uploaded filename.) That means it's time to read some more; say, for example, perldoc perlretut or one of the many nodes here on regular expressions. And then, just for the record, the advice you'll see frequently in what you read here, "Don't use regexen to parse html," refers to the NEXT step of your journey. Using a regex is just fine (at least IMO) to test a filename. | [reply] [d/l] [select] |