$Bill Luebkert wrote: 
> Jim Hill in <[EMAIL PROTECTED]>:
> 
> > I have a log analysis script to which I'm trying to add a pattern
> > matching option. [snip]
> 
> The eval already suggested by Chris should work

It doesn't, unfortunately, unless I'm mishandling it in some way.

> and if you need to
> delay the printing, you could store it back in the hash:
> 
>       $ini{section}{matchedval} = eval "$ini{section}{match}";

In the script proper, the result of the match is concatenated
with an existing text string ...

$txt .= eval "$ini{section}{match}";

This code demonstrates my problem unambiguously, I hope.

# test.pl
use strict;
use warnings;

my %ini;
my $inifile = 'test.ini';
make_ini($inifile);
read_ini($inifile);

# unwrap next three lines
my $line = '0000C000 00000000 18/02/2006 07:44:09 EXCEPTION
(Scripting: Language  not installed) @ Script Execution
[0068DFCD] Recursive call (3 times):';

$line =~ /$ini{test}{regex}/;
print eval "$ini{test}{match}";

sub make_ini {
    my $inifile = shift;
    open INI, ">$inifile" or die "Can't create $inifile: $!";
    print INI '[test]'."\n";
    print INI 'regex=^.*? (..:..:..) .*? \((.*?)\) .*'."\n";
    print INI 'match=$1 $2'."\n";
    close INI or die "Can't close $inifile: $!";
}

sub read_ini {
    my $inifile = shift;
    my $section;
    open INI, "<$inifile" or die "Can't read $inifile: $!";
    while (<INI>) {
        chomp;
        next if /^\s*$/;
        if (/^\[(.*)\]/) {
            $section = $1;
            next;
        }
        my ($field, $value) = split /=/, $_, 2;
        $ini{$section}{$field} = $value;
    }
    close INI or die "Can't close $inifile: $!";
}

Running that using v5.8.7 build 815 on win2k results in ... 

| rdns.org : perl test.pl
| Scalar found where operator expected at (eval 1) line 1, near "$1 $2"
|         (Missing operator before $2?)
| Use of uninitialized value in print at test.pl line 14.

What's confusing me is that the test script works if I modify the
inifile 'match' to include either of $1 or $2 ...

|     print INI 'match=$1'."\n";
| outputs "07:44:09".

|     print INI 'match=$2'."\n";
| outputs "Scripting: Language  not installed".

... but $1 and $2 together just won't work and I can't make sense
of perl's error message either.

Apologies if my responses seem tardy. I'm not seeing messages
from this list today for some reason. I've just picked up your
message from activeperl's website.
-- 

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to