Re: Interpolate variable in a __DATA__ block



"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 ---------
.



Relevant Pages

  • Re: CLOS: static members
    ... I declare it in a base class, it is shared by all subclasses, which is not ... In C++ with templates, you could use the curiously-recurring-template pattern. ... class C: public QObject ...
    (comp.lang.lisp)
  • Re: Dont scrap your boilerplate - Genercism .v. Templates
    ... signature, with templating this should not be so in my view, templates ... should not form part of the signature. ... # Declare methods for the new type ... - An 'attr' parameter requests the name of the given ...
    (comp.lang.misc)
  • Re: Why no language improvements?
    ... Templates are very easy to explain. ... then when you want to use that class with a particular type, say an TObject, ... you could declare a list class like this: ...
    (borland.public.delphi.non-technical)
  • Re: [tip:perf/core] tracing: Add DEFINE_EVENT(), DEFINE_SINGLE_EVENT() support to docbook
    ... Is "declare" here always only used as a declaration? ... is the context of templates? ... that comes the closest to other semantics in the kernel. ... But it requires the helper functions ...
    (Linux-Kernel)
  • Re: Online Manual
    ... It kind of depends on the scope of the project. ... Which version of Publisher are you using? ... Have you looked at the templates in Word? ... It will have a front page and Navigation Links/how ...
    (microsoft.public.publisher.webdesign)