Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re^2: Framing my scripts

by Spidy (Chaplain)
on Jul 27, 2007 at 02:41 UTC ( #629045=note: print w/replies, xml ) Need Help??

in reply to Re: Framing my scripts
in thread Framing my scripts

This looks like a really cool way to do what I'm looking for, except...I don't quite understand it.

Let's say I have 2 scripts I want to implement this way; one will print "Hello World", and the other will print "Foo Bar". How would I set that up using your method?



Replies are listed 'Best First'.
Re^3: Framing my scripts
by kyle (Abbot) on Jul 27, 2007 at 03:07 UTC

    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.

      Excellent advice and presentation++

      Actually HTML::Template forces you to do such a 2-stage transformation.

      If you want to avoid that, you could also use the HTML::Template-compatible HTML::Template::Compiled from tinita, which allows you to specify the innerContent by an additional TMPL-command, see HTC:INCLUDE

      It will expand the embedded template(s) first and do the other template processing on the resulting joined template.

      Not a big issue, but I like the feature.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://629045]
[Lady_Aleena]: I'd have to talk to a geany person to see.
[Lady_Aleena]: Right now, I'm trying to decide whether I want to rewrite how variables are entered in the sub I'm working on.
[shmem]: wait, what? you want that person to read the documentation for you?
[Lady_Aleena]: I might have to go with options instead of a straight list.
[Discipulus]: if more than 3 go for named variables LA
[Lady_Aleena]: Discipulus, it is at 2 now, but with what I am thinking about, it could go to 3. However, only 1 is needed. The second and third are optional.
[shmem]: straight list or named parameters - that depends on whether (and how many) optional arguments you have
[Discipulus]: if so a plain list is ok, imho
[Lady_Aleena]: shmem, I already have the plugin installed, just not active.
[shmem]: if you have 1 optional argument, place that at the end of the list. If you have more, go for named parameters.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2017-04-27 12:19 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (506 votes). Check out past polls.