Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Converting code to use DATA filehandle instead of external templates

by naikonta (Curate)
on Nov 20, 2007 at 06:29 UTC ( #651862=note: print w/ replies, xml ) Need Help??


in reply to resolved: Converting code to use DATA filehandle instead of external templates

(Update, per haoess below) First of all, the DATA filehandle begins after the __DATA__ token, not __END__. Here's one example to do it.
$ cat /tmp/html-data.pl #!/usr/bin/perl use strict; use warnings; use HTML::Template; use File::Basename; my($script, $path) = fileparse($0); # the use of load_page() is intentional my $template = load_page(\*DATA); $template->param( script => $script, path => $path, pid => $$, ); print $template->output; sub load_page { my $fh = shift; HTML::Template->new(filehandle => $fh); } __DATA__ Hi, I'm script <tmpl_var name=script>, located at <tmpl_var name=path> running with process id <tmpl_var name=pid> $ perl /tmp/html-data.pl Hi, I'm script html-data.pl, located at /tmp/ running with process id 4291
Actually, you can also use the scalarref or arrayref options with DATA, such as,
my $template = load_page([<DATA>]); sub load_page { my $stuff = shift; HTML::Template->new(arrayref => $stuff); }
Or,
local $/ = undef; my $template = load_page(\<DATA>); sub load_page { my $stuff = shift; HTML::Template->new(scalarref => $stuff); }

Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!


Comment on Re: Converting code to use DATA filehandle instead of external templates
Select or Download Code
Re^2: Converting code to use DATA filehandle instead of external templates
by haoess (Curate) on Nov 20, 2007 at 08:39 UTC
    First of all, the DATA filehandle begins after the __DATA__ token, not __END__
    That's not always correct. perldoc perldata says:
    __END__ behaves like __DATA__ in the toplevel script (but not in files loaded with "require" or "do") and leaves the remaining contents of the file accessible via "main::DATA".
    $ cat data_eq_end.pl #!/usr/bin/perl use warnings; use strict; print for <DATA>; __END__ 1 2 3 $ perl data_eq_end.pl 1 2 3

    -- Frank

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://651862]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2014-12-20 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (95 votes), past polls