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

Hi,

I have the output of a backquoted command i.e.

$result=`cat $file | anotherscript.pl`;

Does a backquoted command automatically get output to stdout as well as into the string $result?

My question is this: The output looks like:

Script version 1.7

12345 hashes read from
1 database

3 read, 2 found, 2 written
ID123 ZZ00000077
ID456 ZZ00000012
ID124 NONE

What i would like to do is build up a hash of the found items i.e with IDnnn as the key and the ZZ identifier as value. I would also like a hash/array of any NONE found items again with IDnnn as key and NONE as value or if array, just IDnnn pushed onto it.

I dont particularly want the output of the command output to stdout either

I have tried a regular expression to get at the NONE lines such as :

@res = split /\n/, $result; foreach $element (@res){ if ($element=~/(.*NONE)$/m){ push @none, $element; } }

Any help would be appreciated.

Thanks

Joe

Replies are listed 'Best First'.
Re: Regular Expression and backquoted cmds
by rovf (Priest) on Feb 03, 2009 at 11:44 UTC
    Does a backquoted command automatically get output to stdout as well as into the string $result?

    No. You can't have the cake, and eat it too.

    BTW, stderr is caught as well.

    $result=`cat $file | anotherscript.pl`;
    I think this gives you a nomination for the Useless Use Of Cat Award ;-) But from the Perl side, you might consider to assign to @result, not $result, because this spares you from using split afterwards. But don't forget to chomp the lines afterwards. You might also consider something like this:
    # Note: Code untested! my @none=grep { chomp; /NONE$/ } qx(anotherscript.pl <$file);

    And don't forget to use strict and warnings....

    -- 
    Ronald Fischer <ynnor@mm.st>
      BTW, stderr is caught as well.
      No, it's not:
      perl -E '$r = `perl -E"warn qq[Hello]; print qq[world]"`; say "!$r!"' Hello at -e line 1. !world! $
      As you can see, STDERR of the called process is *NOT* caught, only its STDOUT.
        BTW, stderr is caught as well.
        No, it's not:

        You are right. My mistake! Bad mistake! Sorry!

        -- 
        Ronald Fischer <ynnor@mm.st>
Re: Regular Expression and backquoted cmds
by Bloodnok (Vicar) on Feb 03, 2009 at 12:07 UTC
    Something like this should do the trick...
    use warnings; use strict; use Data::Dumper; my @none; my %hash = map { chomp; local @_ = split; if (/\sNONE$/) { push(@none, $_[0]); (); } else { ($_[0] => $_[1]) + }; } grep /^ID\d+/, <DATA>; warn Dumper \%hash, \@none; __DATA__ Script version 1.7 12345 hashes read from 1 database 3 read, 2 found, 2 written ID123 ZZ00000077 ID456 ZZ00000012 ID124 NONE
    returns...
    :!perl test.pl $VAR1 = { 'ID456' => 'ZZ00000012', 'ID123' => 'ZZ00000077' }; $VAR2 = [ 'ID124' ];
    assuming the lines of interest always comprise just 2 whitespace separated fields.

    BTW, as rovf points out, if you want STDOUT populating, you'll have to print each line as it's read from the command ...

    A user level that continues to overstate my experience :-))
Re: Regular Expression and backquoted cmds
by toolic (Bishop) on Feb 03, 2009 at 14:16 UTC
    Does a backquoted command automatically get output to stdout
    Read `STRING` to get a better understanding of what your options are regarding STDOUT and STDERR.