Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Variable Interpolation with %%variable

Reply
Thread Tools

Variable Interpolation with %%variable

 
 
phrankster
Guest
Posts: n/a
 
      08-09-2005
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?

 
Reply With Quote
 
 
 
 
Dave Weaver
Guest
Posts: n/a
 
      08-09-2005
On 8 Aug 2005 21:15:26 -0700, phrankster <(E-Mail Removed)> wrote:
> 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?



 
Reply With Quote
 
 
 
 
Dave Weaver
Guest
Posts: n/a
 
      08-09-2005
On 09 Aug 2005 07:20:06 GMT, Dave Weaver <(E-Mail Removed)> wrote:
>
> while ( <DATA> ) {
> s/\%\%(\w+)/ $lookup{$1} /eg;


That should, of course, be:
s/\%\%(\w+)/$lookup{$1}/g;


 
Reply With Quote
 
ced@carios2.ca.boeing.com
Guest
Posts: n/a
 
      08-10-2005
phrankster wrote:
> 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.

--
Charles DeRykus

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-10-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      08-10-2005

phrankster wrote:
> 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.

 
Reply With Quote
 
Keith Keller
Guest
Posts: n/a
 
      08-10-2005
On 2005-08-10, William James <(E-Mail Removed)> wrote:
>
> 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

--
http://www.velocityreviews.com/forums/(E-Mail Removed)-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom
see X- headers for PGP signature information

 
Reply With Quote
 
James Taylor
Guest
Posts: n/a
 
      08-13-2005
In article <(E-Mail Removed)-francisco.ca.us>,
Keith Keller <(E-Mail Removed)-francisco.ca.us> wrote:
>
> On 2005-08-10, William James <(E-Mail Removed)> wrote:
> >
> > 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.


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.

--
James Taylor, London, UK PGP key: 3FBE1BF9
To protect against spam, the address in the "From:" header is not valid.
In any case, you should reply to the group so that everyone can benefit.
If you must send me a private email, use james at oakseed demon co uk.

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-13-2005
James Taylor wrote:
> Keith Keller wrote:
>> William James wrote:
>>> 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.

>
> 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/...f0dd0686dfab23

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'."

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
James Taylor
Guest
Posts: n/a
 
      08-14-2005
In article <(E-Mail Removed)>,
Gunnar Hjalmarsson <(E-Mail Removed)> wrote:
>
> James Taylor wrote:
> >
> > While I wouldn't wish to encourage William to troll,

>
> That's exactly what you just did, James.


That was always the risk. I apologise.

> > William hasn't yet become offensive about how he presents
> > Ruby snippets

>
> 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.

--
James Taylor, London, UK PGP key: 3FBE1BF9
To protect against spam, the address in the "From:" header is not valid.
In any case, you should reply to the group so that everyone can benefit.
If you must send me a private email, use james at oakseed demon co uk.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
heredoc with variable interpolation breaks script Oliver Heins Ruby 2 09-11-2006 03:59 PM
How to stop interpolation of Escape character in variable? \Rob\ ASP .Net 8 02-24-2006 08:52 AM
variable interpolation failed :-( fred Perl Misc 20 04-26-2004 08:25 PM
how to resolve a variable but disallow interpolation in regex match Praveen Kallakuri Perl Misc 3 04-15-2004 02:29 AM
Variable interpolation question Andrew Fabbro Python 3 11-17-2003 07:44 PM



Advertisments