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

I want to embed some custom html tags into a web page that gets displayed via a cgi script. The custom tags are to access the various features of the script. I would like to use a format like:
<myscriptname type="type_of_field" value="list,of,values">

Is there a module for this, or would it be better to recognize the <myscriptname and then just parse the rest out with regex's?

The module would have to be cross platform of course ...

Replies are listed 'Best First'.
Re: Custom HTML Tags
by shotgunefx (Parson) on Apr 26, 2002 at 11:49 UTC
    Use HTML::Parser like so.
    #!/usr/bin/perl -w use strict; use HTML::Parser; my $p = HTML::Parser->new( start_h => [\&Start, 'tagname, attr, text '], end_h => [sub { print shift; }, 'text'], comment_h => [sub { print shift; }, 'text'], default_h => [sub { print shift; }, 'text'], ); open (IN, "<form.template") or die "Couldn't open!:$!"; # Your +html my $content = ''; { local $/=undef; $content = <IN>; } $p->parse($content); close(IN) or die "Couldn't close:$!"; ################################################ sub Start{ my ($tag, $attr,$text) = @_; if ($tag eq 'mytag'){ $text = "WHATEVER YOU WANT TO REPLACE HERE. } print $text; }

    Update
    Just FYI, $attr in start is a hashref with keys that are the lc attributes of the tag and values which are, the values.

    -Lee

    "To be civilized is to deny one's nature."
Re: Custom HTML Tags
by davorg (Chancellor) on Apr 26, 2002 at 14:17 UTC

    CGI.pm handles custom HTML tags just fine. You need to add the custom tag names to the list of function names imported from the module and it will synthesis functions for the new tags.

    use CGI qw(:standard myscriptname); print myscriptname({type => 'type_of_field', value=> 'list,of,values'});

    This prints

    <myscriptname value="list, of, values" type="type" /> --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Custom HTML Tags
by Matts (Deacon) on Apr 26, 2002 at 12:30 UTC
Re: Custom HTML Tags
by cfreak (Chaplain) on Apr 26, 2002 at 19:37 UTC

    Don't get me wrong I'm all for reinventing wheels especially when it helps you to learn something but I would suggest that you take a look at things that have already done this. My personal favorite is HTML::Template

    You might be able to build off of that to add your own tags or just use what's already there

    Some clever or funny quote here.

Re: Custom HTML Tags
by BUU (Prior) on Apr 26, 2002 at 12:28 UTC
    You might want to look at xml instead