Re: Interpolate variable in a __DATA__ block



On Mar 30, 8:13 am, chas.ow...@xxxxxxxxx (Chas. Owens) wrote:
On Sat, Mar 29, 2008 at 9:14 PM, Trudge <tru...@xxxxxxxxx> 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. 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";
> }

> --
> Gunnar Hjalmarsson
> Email:http://www.gunnar.cc/cgi-bin/contact.pl

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

For the curious, I find myself sometimes having to display similar
blocks of text over and over, with only minor changes. It could be
HTML or XML. 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.

Thanks to all who responded.

snip

If you have multiple templates you might want to use Inline::Files*
instead of a straight DATA block. This module lets you have multiple
DATA-like blocks:

#!/usr/bin/perl

use strict;
use warnings;
use Inline::Files;

print while <BAR>;
print while <BAZ>;
print while <FOO>;

__FOO__
This is foo
This is still foo
__BAR__
This is bar
This is still bar
__BAZ__
This is baz
This is still baz

*http://search.cpan.org/dist/Inline-Files/lib/Inline/Files.pm
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

I mistakenly posted an incorrect script in my last response. It should
be ...
#!/usr/bin/perl

use strict;
use warnings;
use Inline::Files;

print while <FOO>;
print while <BAR>;
print while <BAZ>;

my $foo="FOO";
my $fooRef=\$foo;

my $bar="BAR";
my $barRef=\$bar;

my $baz="BAZ";
my $bazRef=\$baz;

__FOO__
This is $foo
This is still $$fooRef
__BAR__
This is $bar
This is still $$barRef
__BAZ__
This is $baz
This is still $$bazRef
--
Amer Neely www.webmechanic.softouch.on.ca

.



Relevant Pages

  • Re: Interpolate variable in a __DATA__ block
    ... >> blown template method. ... all contained in one large script. ... If you have multiple templates you might want to use Inline::Files* ... This is still baz ...
    (perl.beginners)
  • Re: Simple script to send mail
    ... Save a Draft message, then duplicate it when needed. ... "Message Template" script by Paul Berkowitz ... It does not facilitate complex HTML templates with pictures, ... Entourage Help Page ...
    (microsoft.public.mac.office.entourage)
  • Re: Easy way to convert templates to documents?
    ... Paul will be right with you ... ... When you double-click on a .dot in WinWord, ... Otherwise, you will need to wait for Paul's script, which presumably will ... >> from the templates, then deleting the templates, by script, but the only ...
    (microsoft.public.mac.office.word)
  • Re: Offline use of templates
    ... Download all templates from Sharepoint Templates Doc Library to C:\ ... Download global templates that are destined for the Startup ... a little script checks to see if there's a newer version ... templates available than are on the local machine, and if required, ...
    (microsoft.public.word.vba.general)
  • Re: customizing directory folders
    ... I don't know of any way to customize the info for those templates but: ... I've written a script that will apply all the view customizations you can set to all of a folder's ... non-grouped view to be the default inheritance for My Computer, then return My Computer to its ...
    (microsoft.public.windowsxp.general)