From: "Ing. Branislav Gerzo" <[EMAIL PROTECTED]>
> use strict;
> use warnings;
> 
> my $foo = 'test';
> my @bar = ( 'foo', '[%foo%]', 'bar' );
> my @list = ();
> 
> foreach my $x (@bar) {
>         $x =~ s/^\[%([^%]+)%\]$/${$1}/g;
>         print $x . " ";
> }
> 
> -- 
> ...it gives me compilation error:
> Can't use string ("foo") as a SCALAR ref while "strict refs" in use
> at...
> 
> it doesn't work as I expected, I expect output:
> 
> foo test bar

As someone else already told you you need to turn off strict refs for 
a moment for this to work. While that is a solution I don't think 
it's a good one. I don't think you want to be able to interpolate any 
variable at all into the string do you? What if sometime later you 
allow the users of whatever an app to specify the templates? And what 
if they get to see a variable they should not? Let me try "and the 
connection string is [%connection%]." and a few more like this, I 
might get to see your database connection string and then ... ;-)

You most probably want to store the data you want to fill into the 
template in a hash (some call it associative array, please don't):

        my %data = (
                foo => 'test',
                bar => 'other'
        );

        my $string = "Here's [%foo%] and [%bar%] tests.";
        $string =~ s/^\[%([^%]+)%\]$/$data{$1}/g;

Now strict is happy and even if I can specify the template I can't 
see any data you don't want me to :-)

You may also want to read the

        "Why it's stupid to `use a variable as a variable name'" by M-J. 
Dominus at http://www.plover.com/~mjd/perl/varvarname.html

Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to