Bern has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Converting HTML tags to upper case
by marto (Cardinal) on Dec 11, 2006 at 10:30 UTC | |
What have you tried so far? Do you have any working code or is there a particular part of this problem that you need help with? You say that you only have basic Perl knowledge, the Tutorials section of this site is chock full of useful information. You should probably check out Ovid's HTML::TokeParser::Simple module. I am sure that if you Super Search this site, you will be able to find plenty of example code that does what you want, however your question looks like you have posted a specification for a program you need to write, and are waiting for someone to do for you. Update: In addition to the advice I gave you in response to your previous post, Table Help, please read How do I compose an effective node title? Martin | [reply] |
|
Re: Converting HTML tags to upper case
by cdarke (Prior) on Dec 11, 2006 at 10:40 UTC | |
You have a fairly clear spec on what you need, so start by taking that spec as a set of comments. Now "fill in the blanks" using your research and the online documentation, always remembering that "There's more than one way to do it":
Now when you need more specific help with each section you can look up the online documentation and web resources, including SuperSearch. | [reply] [d/l] |
|
Re: Converting HTML tags to upper case
by PockMonk (Beadle) on Dec 11, 2006 at 10:53 UTC | |
If you post up what you've got so far, what you think it should do, and what it actually does do, then people would gladly point out helpful corrections to your code. If you really are very new to Perl, you need to look at "regular expressions". If you don't have a perl book or perl installation with manpages, google search "perl regular expressions". If you're not sure how to tackle this, someone providing you a throwaway solution without you learning anything does you no favours in the long run. Better to learn up on the general principles, have a stab at it, and then get help if your effort doesn't quite work Have a stab at it, and post up with your results :-) | [reply] |
|
Re: Converting HTML tags to upper case
by ww (Archbishop) on Dec 11, 2006 at 16:34 UTC | |
In fact, it's well OffTopic as far as the Perl element of your question But I would recommend, strongly against uppercasing html tags. | [reply] |
|
Re: Converting HTML tags to upper case
by talexb (Chancellor) on Dec 11, 2006 at 18:46 UTC | |
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. | [reply] |
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] |