note
Wonko the sane
A more robust way to do this would be something like this,<br>
that uses a reusable generic function for the actual replacements.<br>
Especially if your template string is going to be dynamic.<p>
<CODE>
#!/usr/local/bin/perl
use strict;
my $filter = q{user ([^\s]+) has ([^\n\r]+)$};
my $log_string = q{user bob has logged on};
my $template = q{user $1 has accessed the system and is $2};
if ( $log_string =~ /$filter/ )
{
# would be better to use something else rather
# than '$1' as a replacement mark
my %vars =
(
'$1' => $1,
'$2' => $2,
);
my $t = interpolate( $template, \%vars );
print qq{LINE: [$t]\n};
}
sub interpolate
{
my ( $text, $vars ) = @_;
study $text;
$text =~ s/\Q$_\E/$vars->{$_}/g for ( keys %{$vars} );
return $text;
} # END interpolate
</CODE>
Wonko
261329
261329