On Wed, Apr 15, 2009 at 09:20, Rajini Naidu <rajinid...@gmail.com> wrote:
> Hi,
>
>  I am trying to execute the below line in the perl program and returns a
> value 0.
>
> my $test2 = "";
> my $include =  "file";
>
> $test2 = system("/usr/atria/bin/cleartool desc \$include | grep created |
> awk -F\" \" \'{print \$2}\' | cut -b 1-9 ");
>
> The value returned by $test2 is 0. I suspect grep and awk commands are not
> getting executed.
> Is there any thing wrong in the syntax ????
>
> -Rajini
>

The system[1] function returns 0 on success and a nonzero value
specific to the operating system on failure (often the programs
exit code and the value returned by exec[2]).  You could use the
qx// operator[3] to capture the STDOUT of the command:

my $test2 = qx{/usr/atria/bin/cleartool desc \$include |
    grep created | awk -F\" \" \'{print \$2}\' | cut -b 1-9};

but a much safer and faster way is to use the open[4] function
and avoid using external programs like grep and awk:

open my $pipe, "-|", "/usr/atria/bin/cleartool", "desc", $include
    or die "could not run cleartool: $!";

my @matches;
while (<$pipe>) {
    next unless /created/;
    my @rec = split;
    push @matches, substr $rec[1], 0, 9;
}

Or if you just want the first match:

open my $pipe, "-|", "/usr/atria/bin/cleartool", "desc", $include
    or die "could not run cleartool: $!";

my $match;
while (<$pipe>) {
    next unless /created/;
    my @rec = split;
    $match = substr $rec[1], 0, 9;
    last;
}

1. http://perldoc.perl.org/functions/system.html
2. http://perldoc.perl.org/functions/exec.html
3. http://perldoc.perl.org/perlop.html#qx/STRING/
4. http://perldoc.perl.org/functions/exec.html

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to