R. Joseph Newton wrote:
>Jan Eden wrote:
>
>>Hi Stuart,
>>
>>@testarray gets the content of testmessage.txt, which contains the
>>string '$name'. You cannot manipulate this string by setting the
>>variable $name. You could do:
>>
>>@testarray =~ s/\$name/$name/g;
>>
>>which will replace the literal string '$name' using your variable's
>>content.
>
>>
>>I am just a beginner myself, and this is not meant to be a cool
>>solution to anything, just a pointer.
>
>Actually, this is an excellent suggestion for using placeholders in
>template files. I would suggest, though, that it would be better to
>use some other delimiter to indicate such place-holder, perhaps
>%placeholdername%., to distinguish these from program variables more
>readily.
From the Cookbook, 1st edition (Recipe 20.9):
sub template {
my ($filename, $fillings) = @_;
my $text;
local $/; # slurp mode (undef)
local *F; # create local filehandle
open(F, "< $filename\0") || return;
$text = <F>; # read whole file
close(F); # ignore retval
# replace quoted words with value in %$fillings hash
$text =~ s{ %% ( .*? ) %% }
{ exists( $fillings->{$1} )
? $fillings->{$1}
: ""
}gsex;
return $text;
}
Works with a hash %fillings containing keys for content, keywords, title etc. and a
template with the appropriate placeholders.
- Jan
--
How many Microsoft engineers does it take to screw in a lightbulb? None. They just
redefine "dark" as the new standard.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>