Lady_Aleena has asked for the wisdom of the Perl Monks concerning the following question:
I discussed this a while ago in the chatter box, but I can't remember what was suggested.
I have a subroutine called print_story which, by default, wraps the lines of my __DATA__ or a file handle in <p></p> tags if the line doesn't start with a number between 1 and 6 or a < which indicates that the line has an HTML tag already. Normally this is all I need, however I've come upon a situation where I need to insert another subroutine into the data to keep the flow of what I'm printing to the screen and to avoid having to make a lot of little files for single sentences.
Here is a sample of use.
#!/usr/bin/perl use strict; use warnings FATAL => qw( all ); use CGI::Carp qw(fatalsToBrowser); use lib '../files/perl/lib'; use Base::HTML qw(print_story); print_story(*DATA,1); __DATA__ This is the first paragraph in the story. This is the last paragraph in the story.
Now, if the story has a definition list in it, I would like to use my subroutine print_definitions in my data to keep the flow.
#!/usr/bin/perl use strict; use warnings FATAL => qw( all ); use CGI::Carp qw(fatalsToBrowser); use lib '../files/perl/lib'; use Base::HTML qw(print_story); print_story(*DATA,1); __DATA__ Paragraph that opens the document. 2 Heading for definition list This is the paragraph that precedes the definition list. #This is the line where the definition list should be inserted. This is the paragraph that follows the definition list. 2 Heading for next definition list This is the paragraph that precedes the next definition list. #This is the line where the next definition list should be inserted. This is the paragraph that follows the next definition list. 2 Heading for the close of the document The paragraph that closes the document.
I've tried preceding the line with a & and then using eval. The only way that it would work is if I used the full name of the subroutine, which in this case would be Base::HTML::print_definitions(). In the future I may want to print a list of links or use another subroutine I am writing for printing tables. I really don't want to have to use the full names of each subroutine which is part of the text.
The following is the code for both print_story and print_definitions which prints the results I expect by themselves. However, I just haven't figured out the best way of getting them to work together. start_html and end_html are the templates I use on every page of my site. The line subroutine just adds tabs to the beginning of a line and then prints a newline after the line. get_hash does what it says, it gets a hash from a data file.
sub print_story { my ($source,$html) = @_; my $tab = $html ? 3 : 4; start_html() if $html; while (my $line = <$source>) { chomp($line); if ($line =~ m/^</) { line($tab,$line); } elsif ($line =~ /^[1-6]\s/) { my ($heading,$text) = split(/ /,$line,2); line($tab,qq(<h$heading>$text</h$heading>)); } else { line($tab,qq(<p>$line</p>)); } } line($tab,qq(<p class="author">written by $user</p>)) if $tab == 3; end_html if $html; } sub print_definitions { my (%opt) = @_; my $tab = exists($opt{html}) ? 3 : 4; my %definition_list; get_hash( hash => \%definition_list, file => exists($opt{file}) ? $opt{file} : get_data, headings => [@{$opt{headings}}], sort => 'yes', ); start_html() if exists($opt{html}); unless (exists($opt{html}) || exists($opt{heading})) { line(3,qq(<h2>$heading definitions</h2>)); } line($tab,q(<dl>)); my $term = shift @{$opt{headings}}; for my $term (sort {$definition_list{$a}{sort_number} <=> $definitio +n_list{$b}{sort_number}} keys %definition_list) { line($tab + 1,qq(<dt>$term</dt>)); if (scalar @{$opt{headings}} == 1) { line($tab + 2,qq(<dd>$definition_list{$term}{$opt{headings}->[0] +})); } else { for my $heading (@{$opt{headings}}) { my $upheading = ucfirst $heading; line($tab + 2,qq{<dd><b>$upheading:</b> }.encode_entities($def +inition_list{$term}{$heading}).qq{</dd>}); } } } line($tab,q(</dl>)); end_html if exists($opt{html}); }
|
---|