Variable Interpolation with %%variable

Discussion in 'Perl Misc' started by phrankster, Aug 9, 2005.

  1. phrankster

    phrankster Guest

    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?
    phrankster, Aug 9, 2005
    #1
    1. Advertising

  2. phrankster

    Dave Weaver Guest

    On 8 Aug 2005 21:15:26 -0700, 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?


    "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?
    Dave Weaver, Aug 9, 2005
    #2
    1. Advertising

  3. phrankster

    Dave Weaver Guest

    On 09 Aug 2005 07:20:06 GMT, Dave Weaver <> wrote:
    >
    > while ( <DATA> ) {
    > s/\%\%(\w+)/ $lookup{$1} /eg;


    That should, of course, be:
    s/\%\%(\w+)/$lookup{$1}/g;
    Dave Weaver, Aug 9, 2005
    #3
  4. phrankster

    Guest

    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
    , Aug 10, 2005
    #4
  5. phrankster

    Paul Lalli Guest

    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
    Paul Lalli, Aug 10, 2005
    #5
  6. 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.
    William James, Aug 10, 2005
    #6
  7. phrankster

    Keith Keller Guest

    On 2005-08-10, 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.

    --keith

    --
    -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
    Keith Keller, Aug 10, 2005
    #7
  8. phrankster

    James Taylor Guest

    In article <-francisco.ca.us>,
    Keith Keller <-francisco.ca.us> wrote:
    >
    > On 2005-08-10, 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, 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.
    James Taylor, Aug 13, 2005
    #8
  9. 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/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'."

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Aug 13, 2005
    #9
  10. phrankster

    James Taylor Guest

    In article <>,
    Gunnar Hjalmarsson <> 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.
    James Taylor, Aug 14, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. \Rob\
    Replies:
    8
    Views:
    614
    Daniel Kirsch
    Feb 24, 2006
  2. Andrew Fabbro

    Variable interpolation question

    Andrew Fabbro, Nov 17, 2003, in forum: Python
    Replies:
    3
    Views:
    286
    anton muhin
    Nov 17, 2003
  3. Oliver Heins
    Replies:
    2
    Views:
    115
    Oliver Heins
    Sep 11, 2006
  4. Praveen Kallakuri
    Replies:
    3
    Views:
    155
    Praveen Kallakuri
    Apr 15, 2004
  5. fred

    variable interpolation failed :-(

    fred, Apr 21, 2004, in forum: Perl Misc
    Replies:
    20
    Views:
    206
    Chris Marshall
    Apr 26, 2004
Loading...

Share This Page