Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
more useful options
 
PerlMonks  

Re^2: updating a variable call in a hash table

by coopermc (Initiate)
on Feb 09, 2009 at 03:24 UTC ( #742332=note: print w/ replies, xml ) Need Help??


in reply to Re: updating a variable call in a hash table
in thread updating a variable call in a hash table

Is there no simple way to do this w/o invoking a module?


Comment on Re^2: updating a variable call in a hash table
Re^3: updating a variable call in a hash table
by coopermc (Initiate) on Feb 09, 2009 at 03:37 UTC
    I guess here's an even simpler example:
    $color = "green"; $text = "The color is $color" $color = "blue"; print $text;
    I want the output to be "The color is blue". How do I make that happen?

      It doesn't matter where your template string comes from, it's still a string containing some markup.

      It's the same question with the same answer: use a templating system. Consider:

      use warnings; use strict; for my $color (qw(green blue red puce)) { my $text = "The color is COLOR\n"; $text =~ s/COLOR/$color/g; print $text; }

      prints:

      The color is green The color is blue The color is red The color is puce

      Perl's payment curve coincides with its learning curve.
Re^3: updating a variable call in a hash table
by GrandFather (Cardinal) on Feb 09, 2009 at 03:43 UTC

    You could include some markup text in the string. When you come to render the HTML replace the markup text with the variable value. In its simplest version that could just entail using a regex.

    But of course, what you are building is your own template system. When you need to go beyond a couple of trivial variable substitutions you'll realize you should have used a module in the first place. Being really lazy often entails a little more work up front.


    Perl's payment curve coincides with its learning curve.
Re^3: updating a variable call in a hash table
by gnosti (Pilgrim) on Feb 09, 2009 at 03:57 UTC
    Is there no simple way to do this w/o invoking a module?

    Sure there is!

    $sentence = q("The next color is $color."); $color = q(blue); print eval $sentence;
    You can learn how to be lazy later :-)

    Note: There can be security implications if $color is supplied by user input.

      Aha, that is what I was after. Thank you.
      Note: There can be security implications if $color is supplied by user input.
      Famous last words!

      No need for eval.

      $sentence = sub { "The next color is ". shift }; $color = q(blue); print $sentence->($color);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://742332]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (18)
As of 2014-04-17 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (449 votes), past polls