develooper Front page | perl.beginners | Postings from March 2008

Interpolate variable in a __DATA__ block

Thread Next
From:
Trudge
Date:
March 29, 2008 12:41
Subject:
Interpolate variable in a __DATA__ block
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. The following code only goes so far. It
correctly prints the value of $$dataref each time GetData is called,
but not in the while loop. This is where I'm stuck.
8<---------------------
#! /usr/bin/perl
use strict;
use warnings;

my @Blocks=qw(first second third);

foreach my $block (@Blocks)
{
	GetData($block);
}

sub GetData
{
	my $data=shift;
	my $dataref=\$data;
	print "\$\$dataref is: $$dataref\n";

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


__DATA__
<first>
This is the $$dataref chunk.
</first>
<second>
This is the $$dataref chunk.
</second>
<third>
This is the $$dataref chunk.
</third>
8<------------------------


Thread Next


Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About