kitsonrp has asked for the wisdom of the Perl Monks concerning the following question:

#the next 2 line have been assigned earler in the program from reading + in the config file $filter = "user (.*) has (logged on)"; $output_string = "user $1 has accessed the system and is $2"; #this line has been read in from a log file $log_string = "user bob has logged on"; # need to search the log string and format output string replacing $1 +& $2 $output_string =~s/filter//g; $output_string =~s/(\$\w+)/$1/eeg; print "$output_string\n";
output is "user has accessed the system and is"
should be "user bob has accessed the system and is logged on"
looks like the output string needs to evaluate $1 & $2 how can this be done ? I know that if the output string is assigned after $1 is assigned it will work, but I needs to do this in the order specified

Replies are listed 'Best First'.
Re: How can I use $1 in an assigned string
by chromatic (Archbishop) on May 28, 2003 at 16:21 UTC
    my $make_output = sub { "user $_[0] has accessed the system and is $_[ +1]" }; # apply regex to filter my $output_string = $make_output->( $1, $2 );
Re: How can I use $1 in an assigned string
by Wonko the sane (Curate) on May 28, 2003 at 15:28 UTC
    There are more robust ways to do this, but this does what you want.

    I changed your filter pattern a bit to make it more robust.
    #!/usr/local/bin/perl use strict; my $filter = q{user ([^\s]+) has ([^\n\r]+)$}; my $log_string = "user bob has logged on"; $log_string =~ s/$filter/user $1 has accessed the system and is $2/; print qq{LINE: [$log_string]\n};

    Outputs
    :!./t2.pl LINE: [user bob has accessed the system and is logged on]
    Wonko
Re: How can I use $1 in an assigned string
by Wonko the sane (Curate) on May 28, 2003 at 16:01 UTC
    A more robust way to do this would be something like this,
    that uses a reusable generic function for the actual replacements.
    Especially if your template string is going to be dynamic.

    #!/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
    Wonko
Re: How can I use $1 in an assigned string
by blokhead (Monsignor) on May 28, 2003 at 16:27 UTC
    A fun way to accomplish this is with DynScalar from our very own japhy.
    use DynScalar; my $filter = "user (.*) has (logged on)"; my $output_string = dynamic { "user $1 has accessed the system and is +$2\n" }; my $log_string = "user bob has logged on"; if ($log_string =~ /$filter/) { print $output_string; }
    I don't know about using this in production code, where someone else has to maintain it. But it's a lot of fun, especially since I was just looking at this module the other day.. And really this is mostly syntactic sugar for chromatic's suggestion, which uses the same concept.

    blokhead

      Thanks, just the solution I was looking for