in reply to I'm drowning in coderefs in my HTML element module
G'day Lady Aleena,
My first thought on this was to have a single, recursive routine that received the tag name as an argument. I had a very brief look at all the threads that had gone before but I don't have time to read them in detail. Consider the following a technique, rather than a solution, which you might be able to integrate into what you have already.
Here's pm_la_html.pl:
#!/usr/bin/env perl use strict; use warnings; my $indent = ' ' x 4; my $web_page = [ [ html => { attr => { lang => 'en' }, content => [ [ head => { content => [ [ title => { content => 'Some Title', }, ], ], }, ], [ body => { attr => { id => 'top' }, content => [ [ header => { }, ], [ article => { }, ], [ footer => { }, ], ], }, ], ], }, ], ]; markup($web_page); sub markup { my ($element_array, $depth) = @_; $depth ||= 0; for my $element (@$element_array) { my ($tag, $data) = @$element; print $indent x $depth, "<$tag"; if (exists $data->{attr}) { for my $key (keys %{$data->{attr}}) { print " $key=\"$data->{attr}{$key}\""; } } if (exists $data->{content}) { print ">\n"; if (ref $data->{content}) { markup($data->{content}, $depth + 1); } else { print $indent x ($depth + 1), "$data->{content}\n"; } print $indent x $depth, "</$tag>\n"; } else { print " />\n"; } } return; }
Here's the output:
$ pm_la_html.pl <html lang="en"> <head> <title> Some Title </title> </head> <body id="top"> <header /> <article /> <footer /> </body> </html>
-- Ken
|
|---|