Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Embed a template in perl file

by whedglon (Initiate)
on Jan 06, 2012 at 20:25 UTC ( #946661=perlquestion: print w/ replies, xml ) Need Help??
whedglon has asked for the wisdom of the Perl Monks concerning the following question:

Greetings! I am interested in embedding template files (that happen to be XML) into a perl script. Currently, my perl script will go to a folder named TEMPLATES, open a template file (based on the user's input) and load the file into an array. Instead of having a separate TEMPLATES folder to hold the various template files; I would like to "embed" them directly into the perl code. Does anyone have any experience with this or thoughts on how to achieve it? Thanks!

Comment on Embed a template in perl file
Re: Embed a template in perl file
by Eliya (Vicar) on Jan 06, 2012 at 21:07 UTC
Re: Embed a template in perl file
by luis.roca (Deacon) on Jan 06, 2012 at 21:14 UTC

    Well, one of the main reasons to have templates is to create a separation of concerns. Mashing everything together seems counter to that (if I'm understanding you correctly). Other than Eliya's suggestion I can only think of something like Text::Template (There's also Template::Toolkit but that's definitely going to be more than one file) having the ability to mix and embed in that way but you will still have two files. Sorry I can't be more help. Maybe with a little more clarity a few other monks can assist you.

    "...the adversities born of well-placed thoughts should be considered mercies rather than misfortunes." Don Quixote
Re: Embed a template in perl file
by graff (Chancellor) on Jan 07, 2012 at 04:28 UTC
    I would like to "embed" them directly into the perl code.

    And what sort of benefit do you expect to get from doing it this way? What is it about your existing setup (using a separate "templates" directory to store templates) that you think needs to be "fixed"? (How is it "broken" in your opinion?)

      Hi All, Thanks for the comments, I'll give those two options a shot.

      A bit more background: I release the application to users as a .exe file (using PAR::packer). My user base is less computer savvy and they tend to change the template files if they have access to them. The solution would be much cleaner (and safer) if I could "hide" the templates from users by wrapping them up into the perl file.

      Thanks again!

        Ah. So you want to be the benevolent dictator who stifles creative liberty in order to protect users from themselves -- you'd rather not allow them to make all those horrible mistakes that they certainly can't help making when they dare to presume that they can improve on your work and/or make your code more suitable to their individual needs... (I'm joking, of course, but only because your reasoning is kind of amusing.)

        UPDATE: Just one point to clarify my motivation in poking fun at your plan: In order to make modifications harder for end-users, it's normally true that you end up making it harder in general. If you feel that the benefit of centralized control will justify the cost of the added maintenance load, have at it.

        My user base is less computer savvy and they tend to change the template files if they have access to them.

        If they're smart enough to find the templates, I say they are computer savvy :D

        Any what

        #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw' pp '; use File::Slurp qw' read_file '; use autodie qw' chdir open '; chdir 'templates directory'; open my($fh), '>:raw', 'MahTemplates.pl'; select $fh; print '%templates = (', "\n"; for my $file ( glob '*.xml' ){ print dd( $file, scalar read_file( $file, { binmode => ':raw' } ) ), "\n"; } print "\n);\n"; close $fh;
Re: Embed a template in perl file
by molecules (Monk) on May 01, 2014 at 20:20 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2014-07-12 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (240 votes), past polls