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

I'm working on a engine that requires tokens. The code snippet below is self explanatory. I am trying to get the script to print out the value of $test_var. It is printing out $test_var as $ticket_price as oppose to $test_var as 2. Any suggestions?
#!/usr/bin/perl -w $seller_state = "CA"; $token = "##ticket_price##"; $rule = "if(##seller_state## eq 'CA'){ ##ticket_price## = 2; }"; token_clean($rule); eval $rule; warn $@ if $@; $test_var = token_clean($token); print "$test_var\n"; #doesnt work print "$ticket_price\n"; #work sub token_clean(){ $_[0] =~ s/##(.+?)##/\$$1/g; return($_[0]); }

Replies are listed 'Best First'.
Re: Token Replacement
by Aristotle (Chancellor) on Jun 18, 2002 at 04:51 UTC
    You're mixing the scopes of your "rules" and your regular code, in that snippet - unless you really want that (why one would eludes me, but there may be good reason I cannot think of), you will probably run into huge headaches at some point with that approach. If you don't understand why I'm saying this, then I urge you for your own sanity (and/or that of the people who will have to work with your code after you) to stick to a tried a and true solution from the CPAN. Text::Template for example handles templates with embedded Perl code very well.

    Makeshifts last the longest.

Re: Token Replacement
by Anonymous Monk on Jun 18, 2002 at 00:22 UTC
    figured out. had to put eval around variable to print
      Well, i am glad you got it to work, but maybe, just maybe, this is not the best way to solve your problem. Me? I'd tackle it with a +200 CPAN module named Parse::RecDescent, but that's just me ...

      UPDATE:
      ... that is, if you are doing what i think you are doing ... what are you doing? That is some of the craziest Perl code i have seen in a while, FWIIW.

      I am impressed, i think ...

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
A reply falls below the community's threshold of quality. You may see it by logging in.