tags:

views:

52

answers:

2

Having trouble finding the correct syntax for creating a Definition List via Perl to create output as follows:

<dt>One</dt> 
        <dd><p>Testing 1</p></dd> 
<dt>Two</dt> 
        <dd><p>Testing 2</p></dd> 
<dt>Three</dt> 
        <dd><p>Testing 3</p></dd> 
</dl> 

I can't seem to find any documentation on the usage. I tried $cgi->dl($cgi->dt([One,Testing1,Two,Testing2,Three,Testing3]))); and other variations but so far no luck. Searching Google or perldoc haven't helped so far.

+3  A: 
print $cgi->dl(
        $cgi->dt('One'),
        $cgi->dd(
                $cgi->p('Testing 1')
        ),
        $cgi->dt('Two'),
        $cgi->dd(
                $cgi->p('Testing 2')
        ),
        $cgi->dt('Three'),
        $cgi->dd(
                $cgi->p('Testing 3')
        ));

I'd really switch to Template-Toolkit instead of generating the data structure using CGI.pm though.

David Dorward
Awesome thanks. Trying to google "Perl cgi Definition List" produced nothing remotely close to what I was looking for. Wouldn't happen to have a good RTFM link would you?I don't know much about Template-Toolkit, Im rather new to Perl, and unless its already installed I dont think its going to be something I can use for this project given that this system is already configured for a specific purpose, and I dont want to run the risk of an outage trying to put something new on there.
http://template-toolkit.org/ — if it isn't installed already, then Local::Lib will let you install it in and any dependencies it needs in a separate library path so it won't interfere with anything else on the system.
David Dorward
+1  A: 

David is correct about the syntax and the suggestion to use Template::Toolkit. Or another templating module.

Here's a simple example that generates a page from a template in the DATA section of a script.

Of course the real power comes when you keep separate template files and reuse and combine them.

#!perl

use strict;
use warnings;

use Template;

my $page_data = {
    title => 'DL Demo',
    data  => [
        {   terms => ['One Term'],
            data  => ['One Definition'],
        },
        {   terms => [qw( Many Terms )],
            data  => ['One Definition'],
        },
        {   terms => ['One Term'],
            data  => [qw( Many Definitions )],
        },
    ],
};

my $tt = Template->new() or die "Ugh";

$tt->process(\*DATA, $page_data);


__DATA__

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
<html>
  <head>
    <title>[% title %]</title>
  </head>
  <body> 
    <div id="header">
      <a href="/index.html" class="logo" alt="Home Page"></a>
      <h1 class="headline">[% title %]</h1>
    </div>

    <div id="data">
      <dl>
         [% FOREACH item = data %] 
         [% FOREACH term = item.terms %] <dt> [% term %] </dt> [% END %]
         [% FOREACH defdata = item.data %] <dd> [% defdata %] </dd> [% END %]
         [% END %]
      </dl>
    </div>


  </body>
</html>

Here's the output:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">
<html>
  <head>
    <title>DL Demo</title>
  </head>
  <body> 
    <div id="header">
      <a href="/index.html" class="logo" alt="Home Page"></a>
      <h1 class="headline">DL Demo</h1>
    </div>

    <div id="data">
      <dl>

         <dt> One Term </dt> 
         <dd> One Definition </dd> 

         <dt> Many </dt>  <dt> Terms </dt> 
         <dd> One Definition </dd> 

         <dt> One Term </dt> 
         <dd> Many </dd>  <dd> Definitions </dd> 

      </dl>
    </div>

  </body>
</html>
daotoad
I'll have to take a look a this, it looks interesting. I dont know if I can go this route on this particular project as the box I am writing a script for has specific packages installed to run a propietary application so I'm trying to work with the modules that already exist (I dont wanna be responsible for breaking something :) Thanks for the info.
@mose, seriously consider using one of the many templating modules on CPAN. If you pick one with slim dependencies it will be easy to distribute it with your script and any other modules it uses. IIRC, HTML-Template has no non-core dependencies (for Perl after 5.8). You should be able to plop it down in your script directory with your other modules and be ready to go.
daotoad