Re: Interpolate variable in a __DATA__ block



Gunnar Hjalmarsson wrote:

Trudge wrote:

I'm trying to get a script to interpolate variable values in a
__DATA__ block if possible. This is a kind of alternative to a full-
blown template method. I'm not sure if I can even do what I want,
hence my posting here.

It can be done; see the FAQ entry

perldoc -q "expand variables"

<snip>

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

Try to replace that with:

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

Of course it can be done explicitly, but I assume the OP was asking if
it could be done implicitly, as if it were in double quotes. All you've
done here is write a Perl compiler for the specific case of embedded
scalar references; if you can write something to 'interpolate variable
values in a __DATA__block' in general then I'll be impressed.

Rob
.



Relevant Pages

  • Re: Why must I chomp a variable set in a script?
    ... If they are of zero size, ... it is supposed to trigger the email program to notify the users. ... have to "chomp" the variable to get this to work? ... like the value was supplied from the command line, it is set in the script. ...
    (perl.beginners)
  • Re: Why must I chomp a variable set in a script? (Problem Solved)
    ... Why must I chomp a variable set in a script? ... On 6/18/07, Paul Lalli wrote: ... the chomp is no longer in the script as it didn't seem to resolve this ...
    (perl.beginners)
  • Re: Extracting fields from a data file
    ... my script works properly now: ... chomp $line; ... close INDATA; ...
    (perl.beginners)
  • Re: Dont understand the errors
    ... This script was a way to keep up and expand somewhat, ... Why chomp the newlines off and then just add them back on again? ... If you were writing this ten years ago you would now have a negative ... my $PaddedDateStr = sprintf "%02d%02d%02d%s%02d%02d%02d", ...
    (perl.beginners)
  • Sum not producing zero
    ... Below is a small script and results... ... doing wrong - the sum should produce zero but does not. ...
    (perl.beginners)