Variable Interpolation with %%variable

P

phrankster

I'm trying to figure this out. I have a XML file that is being read in
that contains %%VARIABLE syntax. What I need to be able to accomplish
is replacing %%VARIABLE with $VARIABLE.

For example:

$VARIABLE = "hello world.";

$line = "%%VARIABLE this is a test."
$line =~ s/(\%\%\w+)/$1/gee;

This does not seem to work. Any ideas?
 
D

Dave Weaver

I'm trying to figure this out. I have a XML file that is being read in
that contains %%VARIABLE syntax. What I need to be able to accomplish
is replacing %%VARIABLE with $VARIABLE.

For example:

$VARIABLE = "hello world.";

$line = "%%VARIABLE this is a test."
$line =~ s/(\%\%\w+)/$1/gee;

This does not seem to work. Any ideas?

"does not work" is the poorest kind of problem description possible!
You should state what results you expect, what your code gives you,
and how they differ, if it's not obvious. You should also include a
*short* but *complete* program that demonstrates the problem so that
others can replicate it with minimal effort. Please consult the
posting guildlines that are regularly posted to this group for
information on how you can compose your questions to get the best
possible replies.

In this case, you need a hash (the solution to many problems).

--8<------------

#!/usr/bin/perl
use strict;
use warnings;

my %lookup = (
VARIABLE => 'hello world.',
DATE => 'today',
);

while ( <DATA> ) {
s/\%\%(\w+)/ $lookup{$1} /eg;
print;
}

__DATA__
the variable is --%%VARIABLE--
It is now %%DATE.

--8<------------

However, there are many templating systems available on CPAN - are you
sure you're not just re-inventing the wheel?
 
C

ced

phrankster said:
I'm trying to figure this out. I have a XML file that is being read in
that contains %%VARIABLE syntax. What I need to be able to accomplish
is replacing %%VARIABLE with $VARIABLE.

For example:

$VARIABLE = "hello world.";

$line = "%%VARIABLE this is a test."
$line =~ s/(\%\%\w+)/$1/gee;

This does not seem to work. Any ideas?

Ths substitution hash is clearly the best solution. However,
just in case you ever need the alternative:

$line =~ s/%%(\w+)/eval "\$$1"/eg;

However, it's slow, ugly, and potentially insecure.
Best avoided.
 
P

Paul Lalli

phrankster wrote:
Ths substitution hash is clearly the best solution. However,
just in case you ever need the alternative:

$line =~ s/%%(\w+)/eval "\$$1"/eg;

Not that I would ever recommend this approach, but there's no need to
have the 'eval' keyword actually in there. Just tell the regexp to
eval it twice:

$line =~ s/%%(\w+)/"\$$1"/eeg;
However, it's slow, ugly, and potentially insecure.
Best avoided.

Indeed.

Paul Lalli
 
W

William James

phrankster said:
I'm trying to figure this out. I have a XML file that is being read in
that contains %%VARIABLE syntax. What I need to be able to accomplish
is replacing %%VARIABLE with $VARIABLE.

For example:

$VARIABLE = "hello world.";

$line = "%%VARIABLE this is a test."
$line =~ s/(\%\%\w+)/$1/gee;

This does not seem to work. Any ideas?

You may prefer to use Ruby:

lookup = { 'VARIABLE' => 'Hello, world.' }
line = "%%VARIABLE this is a test."
line.gsub!( /%%(\w+)/ ){ lookup[$1] }
puts line

--> Hello, world. this is a test.
 
K

Keith Keller

You may prefer to use Ruby:

You may prefer to post ruby-related messages to comp.lang.ruby.
comp.lang.perl.misc is not a synonym for comp.lang.ruby.advocacy.

--keith
 
J

James Taylor

You may prefer to post ruby-related messages to comp.lang.ruby.
comp.lang.perl.misc is not a synonym for comp.lang.ruby.advocacy.

While I wouldn't wish to encourage William to troll, I've quite
enjoyed seeing the Ruby snippets he's posted because they have
been focused on the specific task at hand in each thread. This
has allowed a direct comparison of Perl and Ruby syntax and, far
from being impressed by the differences, I am struck by how very
similar the two languages are. Ruby seems to be Perl with neater
syntax; sort of a cross between Perl and Python. Perhaps this
minimal syntax makes it less flexible though, it's hard to judge.

As a general rule, Perl is a magpie language that isn't shy about
copying the best features of other languages. I don't think we need
feel threatened by anyone who says "Here is how language x would do
this". William hasn't yet become offensive about how he presents
Ruby snippets so, although he shouldn't really be posting off-topic,
I feel quite tolerant towards him in the spirit of being open to
new ideas. I just wish he'd comment his code, especially where
Ruby constructs are unfamiliar to Perl programmers.
 
G

Gunnar Hjalmarsson

James said:
While I wouldn't wish to encourage William to troll,

That's exactly what you just did, James.
I don't think we need feel threatened by anyone who says "Here
is how language x would do this".

Threatened? No. Not all off-topic posts are threatening.
William hasn't yet become offensive about how he presents
Ruby snippets

Wake up, please! It's apparent that he is trolling, and the most
offensive post so far can be found in this thread:
http://groups-beta.google.com/group/comp.lang.perl.misc/browse_frm/thread/e3f0dd0686dfab23

Paul's pointing out that open questions without code are likely to be
answered with pointers to the docs was commented by William with: "note
the arrogance and rudeness of 'Perl gurus'. They will condescend to help
you only when they are 'bored'."
 
J

James Taylor

That's exactly what you just did, James.

That was always the risk. I apologise.
Paul's pointing out that open questions without code are likely
to be answered with pointers to the docs was commented by
William with: "note the arrogance and rudeness of 'Perl gurus'.
They will condescend to help you only when they are 'bored'."

Yeah, well, perhaps William misunderstands why the regulars here
tend to be brusque with newbies asking questions covered by the FAQ.
It's not that people here don't wish to help newbies, it's that
maintaining the FAQ represents considerable work, so it's
exasperating when they don't take the trouble to read it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top