http://www.perlmonks.org?node_id=946714


in reply to Embed a template in perl file

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?)

Replies are listed 'Best First'.
Re^2: Embed a template in perl file
by whedglon (Initiate) on Jan 07, 2012 at 05:38 UTC
    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;