In "Hello World":
use My::Wrapper qw( wrap );
wrap( \&hello_world );
sub hello_world {
my %params = %{shift()};
my $T_ref = $params{T};
$T_ref->{msg} = "Hello World";
return;
}
In "Foo Bar":
use My::Wrapper qw( wrap );
wrap( \&foo_bar );
sub foo_bar {
my %params = %{shift()};
my $T_ref = $params{T};
$T_ref->{msg} = "Foo Bar";
return;
}
Then the main module.
package My::Wrapper;
use strict;
use warnings;
use Exporter 'import';
@EXPORT_OK = qw( wrap );
use DBI;
use CGI;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use HTML::Template;
use HTML::Entities;
use lib 'code';
use config;
## Security
$CGI::DISABLE_UPLOADS = 1; # no uploads!
$CGI::POST_MAX = 512*1024; # max 512k post
sub wrap {
my ( $code_ref ) = @_;
my $template = templates::loadTemplate('template');
my %T;
my $innerTemplate;
my $q = new CGI;
my $c;
$code_ref->({ q => $q,
T => \%T,
# etc.
});
## Filling in the template here
if($innerTemplate) {
# load the template in, put it into %T.
my $template = templates::loadTemplate('$innerTemplate');
$template->param($T{innerContent}) if $T{innerContent};
$T{content} = $template->output();
}
print $q->header();
$template->param(%T);
print $template->output();
}
I don't know anything about your template, and I'm not sure what I can deduce from what you've posted. If you want each script to have its own template, then you have to let the wrapped function give that back to the wrapper somehow. You could do this by having a special element in %T or have the wrapped code pass out a hash ref with whatever it wants to supply.
Also, it's important to note that the above code is not tested or anything. When I do this sort of thing myself, I use objects. In that case, I don't go passing things around; the object just sets its own attributes for communication.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.