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

Ok say file.htm had a thing called %!poll!%, and in my script I wanted to replace that with a subroutine, that looks like this.
sub poll_view { $sth = $dbh->prepare("SELECT * FROM poll WHERE status = 'active'"); $sth->execute or die $dbh->errstr; @slog = $sth->fetchrow_array; print qq~ <table align="center" width="100%" cellpadding="4" cellspacing="0"> <tr> <td class="darktext"> <font size="2"><b>$slog[1]</b></font> <table width="100%" cellpadding="2" cellspacing="2"> ~; poll_do_it("darktext"); print qq~ </table> </td> </tr> </table> ~; } sub poll_do_it { ($ab) = @_; for($i=2; $i <= 6; $i++) { if($slog[$i]) { $a = $i + 5; print "<tr><td width=\"190\" class=\"$ab\">$slog[$i]</td> <td width=\"10\" class=\"$ab\"> $slog[$a]</td>"; if($slog[$a] eq "0") { $fin = "0.00%"; $num = 0; } else { $fin = sprintf("%.2f%", ($slog[$a]/$slog[12])*100); $num = sprintf("%.0f", ($slog[$a]/$slog[12])*150); } print "<td width=\"160\"> <img src=\"images/left.gif\" width=\"2\" height=\"5\"><img +src=\"images/poll.gif\" width=\"$num\" height=\"5\"><img src=\"images +/right.gif\" width=\"2\" height=\"5\"></td> <td width=\"10\" class=\"$ab\">$fin</td></tr>"; } } }

Replies are listed 'Best First'.
Re: including subroutines
by grinder (Bishop) on Jul 21, 2002 at 23:36 UTC
    I'm not 100% sure I follow you, but may I assume you are doing some sort of server-side include?

    Let us therefore assume that the file that contains %!poll!% is a template that is read, and the output is sent to the browser.

    In that case you need to open the file, read it line by line, and perform a substitution using a subroutine call. The trick is to use the /e switch on your substitution. Something like:

    s/%!poll!%/poll_view/e

    ... should do the trick nicely. (Assuming the line is being held in $_).

    I note in passing that you could use Perl's alternate quoting mechanisms to good effect. Consider the difference between

    print "<td width=\"160\"> <img src=\"images/left.gif\" width=\"2\" height=\"5\"><img +src=\"images/poll.gif\" width=\"$num\" height=\"5\"><img src=\"images +/right.gif\" width=\"2\" height=\"5\"></td> <td width=\"10\" class=\"$ab\">$fin</td></tr>";

    and

    print qq{<td width="160"> <img src="images/left.gif" width="2" height="5"><img src="i +mages/poll.gif" width="$num" height="5"><img src="images/right.gif" w +idth="2" height="5"></td> <td width="10" class="$ab">$fin</td></tr>};


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: including subroutines
by valdez (Monsignor) on Jul 22, 2002 at 14:04 UTC

    Ciao Anonymous Monk,

    if your goal is to have some sort of SSI, take a look at CGI::SSI or CGI::SSI_Parser. You can borrow some ideas there...

    If you are working or planning to work under mod_perl, Apache::SSI could be the right answer. Personally I enjoyed very much rolling my own solution using HTML::TokeParser: the solution was to search for special tags and substitute them with the output of a subroutine.
    The Eagle Book has a great example of building your own SSI solution with custom libraries of functions.

    Another solution can be HTML::Template::Expr; please refer to its man page for details. BTW, I think that using HTML::Template can help you separating presentation (html) from code.

    Ciao, Valerio