in reply to Re: Unwanted interpolation in backquoted string
in thread Unwanted interpolation in backquoted string

Your "pure-perl" alternative is not exactly equivalent to the OP. First, by default "awk" splits on white-space, just like the default behavior of "split" (so you shouldn't be splitting on commas). Second, if "$file" contains multiple lines that match $vals[0], the OP's backtick version will return a string with multiple lines, whereas your code will only return at most a single line. (Oh, and why do you have "my $val" a second time?) To replicate the "grep | awk" pipeline properly, the sub would actually be simpler:
sub getvalue { my ($file, $val) = @_; my $f = new IO::File $file, "r" or die "Can not open $file: $!"; my $matched = ''; while (<$f>) { $matched .= (split)[1] . "\n" if (/$val/); } return $matched; }

Replies are listed 'Best First'.
Re: Re: Re: Unwanted interpolation in backquoted string
by jdporter (Paladin) on May 04, 2004 at 22:11 UTC
    Or even simpler.
    $good = do { local @ARGV = ($file); join '', map { /$vals[0]/i ? (split)[1]."\n" : () } <>; };
Re: Re: Re: Unwanted interpolation in backquoted string
by Roger (Parson) on May 04, 2004 at 13:12 UTC
    I was just giving an idea on how this could be done, not on the absolute correctness of the code though.