S_Shrum has asked for the wisdom of the Perl Monks concerning the following question:

I posted this about 2 weeks ago but it's not working so I thought I'd start fresh so everyone could see it and input.

I have a CGI created hash (%input) of parameters that are passed to this script. I want to be able to go through the hash and replace matching tokens in a template file with these values.

Example: 'http://someserver/cgi/somecall.pl?file=this.dat', I could then do a search and replace on the $page_template for $file$, where the parameter name is between the wrapper character (in this case '$').

This is what I have currently:

my $w = $input->param('wrapper'); for ( keys %input ) { $page_template =~ s/\Q$w$_$w/$input->param($_)/eg; $page_template =~ s/\Q$w\lc\_$_\E$w/$input->param(lc)/eg; $page_template =~ s/\Q$w\uc\_U$_\E$w/$input->param(uc)/eg; }

...but this is not working.

Note that I am also doing a upper and lower case replacement as well. Parameter names will be appended with a "lc_" or "uc_" depending on the template writers need.

Any help getting this to work would be greatly apprechiated as this is the only thing in my script that I have left to fix (I think) ;-p.

TIA

======================
Sean Shrum
http://www.shrum.net

Replies are listed 'Best First'.
Re: Do regex using hash keys to wrapped template tokens
by little (Curate) on Feb 14, 2002 at 11:08 UTC
    # try this $page_template =~ s/\$(\w+)\$^\w/$input->param($1)/eg; $page_template =~ s/\$(\w+)\$lc_/$input->param(lc($1))/eg; $page_template =~ s/\$(\w+)\$uc_/$input->param(uc($1))/eg;

    Have a nice day
    All decision is left to your taste

      No go. The tokens in $page_template are not being replaced. They are still present in the final display.

      ======================
      Sean Shrum
      http://www.shrum.net

        OK, buddy. :-)
        When you don't describe your problem more precisely, don't read even the faq, don't even take advice given, don't report any observed behaviour of your script nor support any errors occured, well, I can't help you in this case.
        A simple "Go. No Go." is not enough.
        And get rid of that for loop, it seems useless. ;-)

        Have a nice day
        All decision is left to your taste
Re: Do regex using hash keys to wrapped template tokens
by lachoy (Parson) on Feb 14, 2002 at 13:11 UTC

    Not to be a smartass, but is there a reason you're not using something like Template Toolkit? All this nastiness is dealt with, many excellent possibilities are opened up, and you can do more interesting things with your coding life :-)

    Chris
    M-x auto-bs-mode

      the original intent was to keep everything in one script...something as simple as this (I thought) wouldn't really require a mod.

      I'll look into the toolkit anyways.

      Thanx

      ======================
      Sean Shrum
      http://www.shrum.net

Re: Do regex using hash keys to wrapped template tokens
by japhy (Canon) on Feb 14, 2002 at 15:32 UTC
    Ok, your regexes have some errors:
    # use ${w}lc instead of $w\lc # because $w\lc is makes \l make "c" lowercase! $page_template =~ s/\Q$w$_$w/$input->param($_)/eg; $page_template =~ s/\Q${w}lc_\L$_\E$w/$input->param(lc)/eg; $page_template =~ s/\Q${w}uc_\U$_\E$w/$input->param(uc)/eg;
    Those should work you.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who could use a job
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Hmmm...an odd thing is happening. Here is the code:

      if ( $input->param('debug') ) { print "<P>...replacing parameter token +s in final report"; } my $w = $input->param('wrapper'); for ( keys %input ) { if ( $input->param('debug') ) { print "<P>...replacing token: $_"; + } $page_template =~ s/\Q$w$_$w/$input->param($_)/eg; $page_template =~ s/\Q${w}lc_\L$_\E$w/$input->param(lc)/eg; $page_template =~ s/\Q${w}uc_\U$_\E$w/$input->param(uc)/eg; }

      I see the message on screen of "...replacing parameter tokens in final report" but I am not seeing any "...replacing token: __". Is the FOR loop set up correctly? At first I thought I was losing the contents of the hash but I know there are values in $input as the first 'debug' line is working.

      For that matter, I have tried everyones suggestions (thans to all for their time) with no luck meaning the tokens are not replaced in the final template. I'm stumped. I am doing replacements to record and table templates just fine. $page_template is the final to-be-displayed-to-the-user template.

      TIA.

      ======================
      Sean Shrum
      http://www.shrum.net

        Well, what is %input? Methinks you meant for (param) instead of for (keys %input)... sounds like someone's not using strict!

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Do regex using hash keys to wrapped template tokens
by Anonymous Monk on Feb 14, 2002 at 13:07 UTC

    I can see what you are trying to do. You are replacing all occurrences of parameters present in the hash. How do you handle defaults, i.e. %foo% when foo is not passed in. It would make sense to remove these from the template - but perhaps that is not what you want.

    I suggest:

    $page_template =~ s/\$(lc_|uc_)(\w+)\$/{ my $tmp = $input->param($2); ($1 eq 'lc')?lc($tmp): ($1 eq 'uc')?uc($tmp):$tmp; }/eg;

    Apologies if this is not what you want.

    rinceWind

Re: Do regex using hash keys to wrapped template tokens
by particle (Vicar) on Feb 19, 2002 at 01:55 UTC
    based on our chatterbox discussion, you could do something along the lines of...

    #!/usr/local/bin/perl -w use strict; use diagnostics; $|++; my $string = 'test $token1$ $lc_token1$ $token2$ $lc_token2$ end_test' +; my $w = '$'; my $token; for( qw/ token1 token2 / ) { $token = quotemeta $w . $_ . $w; $string =~ s/$token/replace1/g; $token = quotemeta $w . 'lc_' . $_ . $w; $string =~ s/$token/replace2/g; } print $string, "\n";

    ~Particle