£okì has asked for the wisdom of the Perl Monks concerning the following question:

I have a template file that contains a lot of text along with the names of several varriables I will need to use. However, I do not know what those varriables are going to be. They are marked in the template as such: %var_name%. On the other side I have input comming in containing that will give a value to these varriables and then replace them in the template file to make it complete. I have been told many times, my hands slapped for doing so, not to use $$var_name to value the varriables. However, I do no know of a better way. Any ideas or suggestions???

Replies are listed 'Best First'.
Re: Transfering Varriables
by dvergin (Monsignor) on Feb 25, 2002 at 23:38 UTC
    Use a hash to store the substitution values from the one source. Then just sub 'em in. You don't give any information about the source of the values to use for replacements. So I made some assumptions for this quick demo.
    #!/usr/bin/perl -w use strict; my %values; # Pretend we have an input file w/values while (<DATA>) { last if /TEMPLATE/; # needed here to separate demo data my ($key, $val) = split; $values{$key} = $val; } # Pretend we have an input file w/template my $template = join '', <DATA>; # Process the fields for my $key (keys %values) { $template =~ s/%$key%/$values{$key}/g; } # Show result print $template; __DATA__ name Roadrunner job running TEMPLATE Hello Mr. %name%. We hope you are good at %job%.
    You will need to adapt this to make it more robust and to make it fit your need but this should give you the general idea.

    For a more elegant approach to the substitution, replace entire process fields "for" loop with this: $template =~ s/%(\w+)%/$values{$1}/g;

    If you are going to get serious about this, I suggest you look into one of the template modules.

    ------------------------------------------------------------
    "Perl is a mess and that's good because the
    problem space is also a mess.
    " - Larry Wall

(Ovid) Re: Transfering Varriables
by Ovid (Cardinal) on Feb 25, 2002 at 23:36 UTC

    Well, almost any time you want to use soft references ($$var_name), you'll find that a hash does the trick. Beyond that, it's tough to give you an answer. Can you answer the following?

    • What are you using for a template system?
    • How are you passing the data to the template?
    • Could you show us snippets to illustrate each of the above responses?

    If you show us that, we can probably provide better answers.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

•Re: Transfering Varriables
by merlyn (Sage) on Feb 26, 2002 at 00:20 UTC
    Install Template, and you have a ready-made solution:
    use Template; Template->new({ START_TAG => '%', END_TAG => '%', })->process(\*DATA, { <<'END_DATA' =~ /^(\S+)\s+(.*)/gm }); from Randal to Loki subject template processing END_DATA __END__ Here is one way from %from% to %to% to accomplish %subject%.

    -- Randal L. Schwartz, Perl hacker

Re: Transfering Varriables
by blakem (Monsignor) on Feb 25, 2002 at 23:25 UTC
Re: Transfering Varriables
by £okì (Scribe) on Feb 26, 2002 at 00:05 UTC
    Ok, as requested i'm gonna add a lil of my script.
    //extract all varriable names from template file and save names to arr +ay. $clean_vars = gleen_vars(); //extract outgoing mail format from template file. $fd = fopen ($template, "r") or die("Could not open template file"); $mail_out = fread ($fd, filesize($template)); fclose ($fd); //replace varribles in template with varriables from input. $i="0"; $replaced_var = "$$clean_vars"; echo $replaced_var[2][2]; while($clean_vars[2][$i]){ str_replace("%$clean_vars[$i]%","$replaced_var[2][$i]",$mail_out); echo $clean_vars[2][$i]; echo $replaced_var[2][$i]; echo "<br><br>"; $i++; } echo $replaced_var[2][2]; echo $mail_out; break; //seperate out the header information and create varriables to, and fr +om. list($header,$mail_out) = preg_split("%Begin Email%",$mail_out,2); list($to,$from) = preg_split("/[\n]+/",$header); list($crap,$to)=split(":",$to); list($crap,$from)=split(":",$from); //output to mailer deamon sndmail($to,$from,$title,$mail_out); logdata($to,$from,$title,1); /********************************************************************* +***********************/ /* + */ /* gleen_vars function: reads and puts incomming arrays (from template + file) into an array */ /* + */ /********************************************************************* +***********************/ function gleen_vars() { $template = "template.php"; $fd = fopen ("$template", "r") or die("Could not open template fil +e"); $contents = fread ($fd, filesize($template)); fclose ($fd); if(preg_match_all ("/(%) ([_0-9a-zA-Z]*) (%)/x", "$contents", $pre +_vars, PREG_PATTERN_ORDER)){ return($pre_vars); }else{ error("No varriables defined in the template script: $template") +; return(0); } }
Re: Transfering Varriables
by £okì (Scribe) on Feb 26, 2002 at 00:12 UTC
    And my template file looks something like this. text text text text %to% text text text more text %from% text I like text %title% more text etc. etc. etc. %Submittime%