Re: Interpolate variable in a __DATA__ block



Gunnar Hjalmarsson wrote:
Trudge wrote:
On Mar 29, 4:05 pm, nore...@xxxxxxxxx (Gunnar Hjalmarsson) wrote:
Trudge wrote:
I'm trying to get a script to interpolate variable values in a
__DATA__ block if possible.

<snip>

It can be done; see the FAQ entry

perldoc -q "expand variables"

<snip>

while (<DATA>) {
chomp;
next if $_ eq "<$data>";
last if $_ eq "</$data>";
s/(\$\$\w+)/$1/eeg;
print "$_\n";
}

Gunnar, this works perfectly, and is what I am trying to achieve. Many
thanks, and a brew on me :)

<snip>

As far as I know, templates require separate files to
work with. To avoid a lot of separate files, I've been using 'here'
documents up until now, all contained in one large script. But I
wondered if I could achieve a similar
effect putting the blocks of text in a __DATA__ block. Now I know it
can be done, so I will be exploring this method.

Not much to explore, really. Did you read the applicable Perl FAQ? Even if it provides this method, it doesn't exactly recommend it... Besides full-blown template methods, the FAQ suggests a couple of other methods, that usually are better, and so is here documents which you say you have been using.


I've gone through that FAQ and did discover some new things for me. My goal is to be able to have several variables interpolated in the __DATA__ block. As Rob suggested, maybe that is not possible with the method you provided? That is what I will be exploring.

Granted I only gave you a simplistic example to work with, so that may be why you offered that solution.

I've installed the Inline::Files module as suggested by Chas. and will be looking at that as well. The Warning about over-writing files made me wonder though.

Perhaps I'm just as well sticking with my current usage of here-docs.
.



Relevant Pages

  • Re: Captuerd Text as Variable in Search-Operation
    ... this is the most frequently asked single question not ... In some senses it's really just a special case of the existing FAQ ... "How can I expand variables in text strings?" ...
    (comp.lang.perl.misc)
  • Re: strange disk state and corrupt new mail file
    ... Gezelter writes: ... recover some part of the file). ... it may even be in the FAQ]. ... Longer messages are stored in separate files within the same directory ...
    (comp.os.vms)
  • Re: variable interpolation and file read
    ... > Can I force variable interpolation on a file read? ... "How can I expand variables in text strings?" ... However the FAQ answer is incomplete so see also numerousprevious ...
    (comp.lang.perl.misc)
  • Re: perl join on a non printable variable character ?
    ... your $delimiter variable will contain the string consisting of the four ... FAQ: How do I unescape a string? ... Be aware, however, that the FAQ answer to "How can I expand variables ... in text strings?" ...
    (comp.lang.perl.misc)
  • Re: [NEWBIE] Expanding a perl variable twice inside a substitution
    ... Actually this is in some ways just a special case of the FAQ "How can ... I expand variables in text strings?" ... flame-war about why the FAQ maintainers won't put the real answer in ... the FAQ please search for phrase "How can I expand variables in text ...
    (comp.lang.perl.misc)