Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Perl Monks,
I am a noob to the programming of Perl, i have done some practices of Perl but it is still difficult. For example, i am trying to use Perl to write Html, where basically i write a few paragraphs using notepad and then cat it with a Perl program and then it would produce a desired html file of the few paragraphs. the code i have at this point looks like this:

#!usr/local/bin/perl $pattern = "title:"; while (line = <>){ if ($line =~ /$pattern/){ ($garbage, $myTitle) = split(/:/, $line, 2); print "<Html> <Title>$myTitle</Title>"; } } print "<body>" $pattern = "subtitle:"; while (line = <>){ if ($line =~ /$pattern/){ ($garbage, $myParagraph) = split(/:/, $line, 2); print "<body>$my Paragraph" } if (eof) { print "</body>" } } print "</html>"

Since i am not very good at html or perl, i would like to listen to some suggestions for the code from my monk brethrens. An example of a few paragraph to be put on the html may be in the following format: (taking just a few sample quotes from above forum topics)

Title:My Page Subtitle:When somebody buys an item on the list (from a real shop - no +thing to do with the website) they can go onto the site, and change t +he value of that item from "Not bought" to "Bought by person 2" for i +nstance, then click SUBMIT and the HTML will then permanently reflect + the changes so that the item will always have "Bought by person 2" n +ext to it - therefore preventing multiple purchases from other family + members! Does that make sense? I hope so. I know HTML really well, and know how + to install CGI scripts and stuff - It's just writing them which prov +es tricky! Subtitle: Hello great monks. Can someone tell me how to keep perl dbi +silent when something goes wrong? ie.....I do a table cleanup before +I start importing my data. Basically, I just delete what temp tables +are there. If my script crashed on a previous run, it leaves some tem +p tables behind. I need to clear those tables before I can import dat +a. In php, I leave off the "or die()" statement and php doesn't fuss +if those tables are not there. But with perl dbi, it fusses if the ta +bles are missing even with the DIE command missing.

Other than that it can be also helpful if you guys can point out some hints on how to do bold italic or different sized html words using Perl. Thanks once more my Perl Brethrens!

20031125 Edit by Corion: Added formatting (hopefully correct)

Replies are listed 'Best First'.
Re: Using Perl to make Html file
by dragonchild (Archbishop) on Nov 25, 2003 at 20:19 UTC
    First, read the docs for CGI. Then, read the docs for HTML::Template. Try the examples in both. After that, come ask the specific questions you have here.

    Those are two of the standard modules most of us use to deal with HTML from within Perl. There are many others, but I find those to be the best starting point.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Using Perl to make Html file
by davido (Cardinal) on Nov 26, 2003 at 00:28 UTC
    This question seems closely related to the one posted by PaulShotgun under the title Help with updating HTML from drop down menu's please!. And from what I can tell, mostly the same answers were given; read the POD for CGI.pm, read Learning Perl, and ask specific questions. You wouldn't happen to be that same person would you? ;)

    I understand that it can be intimidating coming to grips with what it takes to bring the worlds of Plain Text, Perl, CGI, and HTML together. Each is its own entity, and requires a new and different skillset.

    Let me try to suggest how you might approach your problem.

    First, understand that Perl can be used to read input in the form of text files, HTML, or any of a nearly unlimited number of formats and types of input. However, Perl doesn't inherently know anything about HTML. Perl doesn't even inherently know how to make text pretty with things like bold and italics. All this is stuff you have to know how to do before you can tell Perl how to do it for you.

    Now for program structure.

    By reading your two posts, I get the impression that you would like to have a webpage that has a list of items that family members have requested for Christmas. You also wish to allow other members to select items from that list and check them off as being purchased by mom, dad, or whoever. In your first post you discussed drop-down menus. Let me just make a few suggestions:

    Start with a flat-file that is newline delimited for each record, and :: (double colon) delimited for each field within a record. Each line could contain a gift idea, and its intended recipient (separated by a double colon). After someone has assumed responsibility for buying that item, the purchaser's name will also be included as a third field on the line, again double-colon delimited.

    The first thing the Perl script should do is figure out whether it's being invoked via "submit" button or not. If not, it should read in the datafile, and then generate a webpage showing each gift-idea, the gift's intended recipient, and the person who has assumed responsibility for buying the gift. Any gift/recipient pair that doesn't yet have a purchaser, would perhaps have a drop-down list of possible purchasers next to it. At the bottom of the form, there'll be a submit button.

    On the other hand, if the Perl script is invoked as an HTML Form Action (someone clicked the submit button), the script must ensure that whatever user input came through is valid, and, if so, should update the datafile to add the name of the purchaser next to whatever items that purchaser just accepted responsibility for. The script should then continue by reading in the data file and outputting the same HTML form as described in the previous paragraph.

    That's, as I understand it, the basic premise of your needs. The script is to be executed as a CGI script. It will take input from the datafile, from the web form, and will create new output to the datafile and the browser based on the input.

    This is exactly the job for CGI.pm. You may also find it handy to either use a tied file or a storable datastructure for your data file needs. A single-family-oriented script doesn't need to worry about scalability, so a database may not be necessary, though it certanly wouldn't hurt anything to use a simple database such as AnyData. You will want to be careful about file locking. You will also want to be careful not to allow tainted data into your datafile. Remember that just because you have control over your form, you don't have control over other people who might write their own script to feed their own data to your script, bypassing your form altogether. That's probably unlikely for a simple single-family-oriented script. But it's possible.

    You will also want to exercise caution in parsing the HTML form input. That is why CGI.pm is such a good idea. It does the dirty work for you.

    One observation. If you are struggling with how to output bold and italic words in HTML via a Perl script, the rest of the project may be a little beyond your current skill level. If you dive into the books now you'll probably be able to get it done. But if you don't dig into the books and POD's mentioned by others in this thread and your other thread right now, the project won't be done before Christmas. This isn't terribly intricate stuff, but it does require some investment in time and effort to learn.

    By the way, bold is expressed with a <b></b> tag set in HTML, and can be output via Perl as simply as 'print "<b>Some text</b>\n";'


    Dave


    "If I had my life to live over again, I'd be a plumber." -- Albert Einstein
Re: Using Perl to make Html file
by husker (Chaplain) on Nov 25, 2003 at 21:36 UTC
    I recommend buying a Perl book or two. Although "Perl Programming" (the "camel" book) is the canonical reference, it seems, you might be better served by "Learning Perl" (the llama book).

    "Learning Perl" does have chapters on CGI, among other things.

    After that, start looking at the modules that have already been written for things like generating HTML. Modules are just someone having already done the dirty work for you. :)