in reply to Re^10: RFC: Proofread the POD for my HTML elements module
in thread RFC: Proofread the POD for my HTML elements module

OOPS! I did not update the example code in the synopsis. It has been updated now.

For the other issues, here goes.

  1. You are basically asking for the history of how all of this began about six years ago. You might want to read my home node for the history.
  2. Most other HTML modules use OO. I am a function oriented person and wrote this so I could have easier to use (for me) functions.

Do you know how big my files would be if I statically produced HTML? My site menu alone is 41.2 kb. Keeping it updated across approximately 300 pages would cripple me. When I first started, without being able to produce my site menu with programming, I would have had to quit after only a few pages (since at the time I had a space cap of 10 Mb). Perl has allowed me to keep my file sizes extremely small in some places.

Another bit of history, some did not like seeing me printing HTML directly in my code, and others loathe my line subroutine. So a few months ago, I decided to see if I could start hiding both. One serendipitous side effect of writing this was I was separating my logic from my display, which many here have been telling me to do all along, I just had not seen how to do it. (One script I wrote was three screens long, with this module I was able to reduce it to less than one.)

I can not speak for the other authors, so I will only speak of my own work with head and body.

Here is the template I wrote for my pages using the module...

sub my_html_template { my (%opt) = @_; my $heading = textify(basename($0)) !~ /index/ ? textify(basename($0 +)) : 'My '.lc((split(/\//,cwd))[-1]); my $title = textify(join(' - ',($root_name,map(ucfirst,split(/\//,$r +elative_path))))); $title .= textify(" - $opt{heading}") if $opt{heading}; my $page_heading = $opt{heading} ? $opt{heading} : $heading; my $page_heading_id = idify($page_heading); $page_heading =~ s/_/ /g; html(0, { head => { title => $title, links => [map {{ rel => 'stylesheet', type => 'text/css', href = +> $_ }} get_styles($root_path.'/files/css')], scripts => [{ type => 'text/javascript', src => "$root_link/file +s/javascript/list.js" }] }, body => [ sub { nav(2, sub { heading(3,1,'Site menu', { id => 'Site_menu' }); list(4,'u',get_menu( directory => $root_path, tab => 2, color +=> 0, full => 0 ), { onclick => 'list_onclick(event)' }); link_list(3,qq($root_user off-site),%other_sites); link_list(3,qq(Reciprocated links),%reciprocated_links); }); article(2, sub { heading(3,1,$page_heading, { id => $page_heading_id }); &{$opt{code}}; paragraph(4,"Contact $link{mail}!", { class => 'address' }); # + I should use the address tag here. paragraph(4,'I am against the Internet Blacklist Legislation . +..', { class => 'address' }); }); }], }); }

I went one step further for a lot of my pages with this...

sub story { my ($source) = @_; my $tab = 3; while (my $line = <$source>) { chomp($line); if ($line =~ m/^</) { line($tab,$line); } elsif ($line =~ /^[1-6]\s/) { my ($heading,$text) = split(/ /,$line,2); heading($tab,$heading,$text); } elsif ($line =~ /^[bh]r$/) { line($tab,"<$line>"); } else { paragraph($tab,$line); } } }

So, when I go to display the page, I can do this...

my_html_template( code => sub { story(*DATA) });

Did that cover everything?

Have a cookie and a very nice day!
Lady Aleena

Replies are listed 'Best First'.
Re^12: RFC: Proofread the POD for my HTML elements module
by Don Coyote (Hermit) on May 06, 2013 at 23:49 UTC

    Possibly not the answer you are looking for, but I hope not. I was thinkng on your code and how to bring the program into the functional by means of introducing some kind of presumptive end_tag method. On reflection I realised that this was already in place in the CGI.pm module. Foiled again!

    Now, you mention that you have not fared well with objects so far, and CGI is not an easy beast to tame. At first I also did not comprehend the difference between the functional and the objective approach. Both importing functions and using the object syntax, but I got the script working. With the help of ardous study, and some lenient monks :)

    CGI.pm is both though and the difference in use is derived from how you harness the functions in your script. Either by calling them as functions, thus requiring the need to provide an import list, or in the object method calling way which does not need an import list. A trivial understanding of importing helps here. see Exporter.pm documentation.

    I also think it important to realise that there are many issues in your script that have already been addressed and refined in CGI.pm, for example security, and differing server architectures (eol phrasing). And by using CGI.pm you can gain knowledge of objects, while being productive with functions.

    Here is the example from before, but in CGI functional format. This shows that CGI can be relatively painless. And you can introduce more complex structures as you become familiar with the way of the CGI. In particular the import of a tag preceded by the asterisk chr '*' is probably the key to where i have been attempting to lead you in my previous responses.

    #!/usr/bin/perl -T use strict; use warnings;# qw(fatalsToBrowser warningsToBrowser); # FATAL => qw( a +ll ); #use Base::HTML::Element; #CGI's functional interface, importing a standard set of subroutines #and the specified qw( *tag ) start_tag end_tag switch/pragma/syntax. #read the pod for descriptons of these use CGI qw(:standard *div); #sends HTML to server for humans to read, #comment out to collapse whitespace use CGI::Pretty; print header; #defaults to xml but can be defined to an HTML namespace print start_html( -title=>'My page title', -style=>{'src'=>'css/style.css'}, -script=>{'type'=>'JAVASCRIPT', 'src'=>'javascript/script.js'}, ); # end start_html, auto prints body opentag. print start_div({-class => 'div_class' }); #note start_div - else clos +es print h1({-id => 'sample_heading', -style => 'color:#666' },'My sample + heading 1'); print p('A sample paragraph so you can see how this works.'); #css styling to do print ul({-id=>'colors'}, li({-type=>'disc',-style => 'color:#f00' },'red'), li({-type=>'disc',-style => 'color:#0f0' },'green'), li({-type=>'disc',-style => 'color:#00f' },'blue'), ); print p('Another paragraph for you'); print h2('A second sample heading'); print p('Guacamole or Bust'); print end_div(); # from imported *div flag print p({ id => 'end_remarks', class => 'remarks' }, 'Some end remarks +.'); print end_html();

    Of course learning coding through constructing your own versions is a great way to learn, I too do this. The really hard thing is trying to find something that has not already been done, or at least something you (or I, tbh) may really have a shot at improving on.

    I don't really see CGI going away anytime soon. CGI caters for unknown tags and such. And I'm pretty sure there have been a few changes since last time I viewed it, if only my understanding. But I do start to see possible patch opportunities here and there... well on some modules, mostly doublestuff or refchecking.