Re: Interpolate variable in a __DATA__ block
- From: Uri Guttman <uri@xxxxxxxxxxxxxxx>
- Date: Sun, 30 Mar 2008 16:45:27 GMT
"T" == Trudge <trudge@xxxxxxxxx> writes:
>> Try to replace that with:
>>
>> while (<DATA>) {
>> chomp;
>> next if $_ eq "<$data>";
>> last if $_ eq "</$data>";
>> s/(\$\$\w+)/$1/eeg;
>> print "$_\n";
>> }
T> For the curious, I find myself sometimes having to display similar
T> blocks of text over and over, with only minor changes. It could be
T> HTML or XML. As far as I know, templates require separate files to
T> work with. To avoid a lot of separate files, I've been using 'here'
T> documents up until now, all contained in one large script. But I
T> wondered if I could achieve a similar
T> effect putting the blocks of text in a __DATA__ block. Now I know it
T> can be done, so I will be exploring this method.
why is a __DATA__ block better than a here doc? other than being located
at the bottom of the file, you can only have one DATA block. you can
have multiple here docs and put them anywhere (yes, near the bottom if
you want as long and you figure out how to declare and assign them. not
a big problem). as for templates, there are many templaters that work
from strings and not just files. Template::Simple can do that.
the trick you learned is amusing but very dangerous. it needs direct
access to variables in the current scope. that means you have to declare
and assign all of them in that scope. it is much safer to put all the
template data into a hash or other structure. then you can put the
templates into other modules (not data files) and expand them as
needed. again template::simple makes that easy as to others. you need to
learn more about templating and not be so focused on expanding vars in
the DATA block. you have what we call an XY problem. you think your
problem and solution is X (expanding vars in DATA) when it is actually Y
(how to better handle templating in general).
uri
--
Uri Guttman ------ uri@xxxxxxxxxxxxxxx -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
.
- References:
- Interpolate variable in a __DATA__ block
- From: Trudge
- Re: Interpolate variable in a __DATA__ block
- From: Gunnar Hjalmarsson
- Re: Interpolate variable in a __DATA__ block
- From: Trudge
- Interpolate variable in a __DATA__ block
- Prev by Date: Re: Interpolate variable in a __DATA__ block
- Next by Date: Re: Interpolate variable in a __DATA__ block
- Previous by thread: Re: commify_series script in cookbook page 94
- Next by thread: Re: Interpolate variable in a __DATA__ block
- Index(es):
Relevant Pages
|