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

I'm a new user to perl and have recently inherited the role of administrator with the Rational Clearcase confguration control tool (on NT). I'm trying to write simple scripts to import deliveries of software and I am having problems trying to extract error messages after running cleartool commands. The following is an example. open (TEMP,"cleartool checkout -nc $element_pathname |"),||die "Failed to run cleartool"; while (<\TEMP>){ print "The message is - $_"; ) If the element is not checked out, everythings groovy, I can play with the $_ variable, however if the element is already checked out, the error message is sent to the display, but this appears to be regardless of the print $_ statement, it has not been assigned anything, it doesn't even enter the while loop. This is probabily a simple thing but I'm stumped I need to be able to manipulate this error message. Please Help. P.S. Any other info would be much appreciated

Replies are listed 'Best First'.
Re: Clearcase and Perl Problem
by dchetlin (Friar) on Sep 24, 2000 at 17:54 UTC
    It's difficult to be sure what you want, but I think you're running into the problem that a standard open() call doesn't read STDERR from its child.

    If all you need is to capture STDERR as well as STDIN, you ought to be able to use shell redirection:

    open TEMP,"cleartool checkout -nc $element_pathname 2>&1 |" or die "Failed to checkout: $!"; # Read from TEMP as usual, but now we also have STDERR

    If you need to be able to distinguish between STDIN and STDERR, your best bet is probably to use IPC::Open3.

    use IPC::Open3; my $pid = open3 IGNORE,TEMPOUT,TEMPERR, 'cleartool',"checkout -nc $element_pathname" or die "Failed to open3: $!"; # first read from TEMPERR while (<TEMPERR>) { # open3 returns an exec error on the error fh you gave it /^open3:/ && die "Error in open3: $_"; # Check for the cleartool error message here } while (<TEMPOUT>) { print "The message is - $_"; } # Don't forget to reap it! waitpid $pid,0;

    Possibly easier would be using shell redirection to direct each of STDOUT and STDERR into files, and then reading from the files. You might check out perlfaq8 for general info on this stuff, although I couldn't find anything that had a decent open3 example.

    echo "Get some open3 examples in the docs\n" | cat >>TODO

    -dlc

RE: Clearcase and Perl Problem
by turnstep (Parson) on Sep 24, 2000 at 16:00 UTC

    Translated:

    I'm a new user to perl and have recently inherited the role of administrator with the Rational Clearcase confguration control tool (on NT). I'm trying to write simple scripts to import deliveries of software and I am having problems trying to extract error messages after running cleartool commands. The following is an example.

    open (TEMP,"cleartool checkout -nc $element_pathname |"),||die "Failed + to run cleartool"; while (<\TEMP>){ print "The message is - $_"; )

    If the element is not checked out, everythings groovy, I can play with the $_ variable, however if the element is already checked out, the error message is sent to the display, but this appears to be regardless of the print $_ statement, it has not been assigned anything, it doesn't even enter the while loop. This is probabily a simple thing but I'm stumped I need to be able to manipulate this error message. Please Help.
    P.S. Any other info would be much appreciated

    Quick notes: I hope the comma before the "|| die" and the backslash before the TEMP are typos.

RE: Clearcase and Perl Problem
by Shendal (Hermit) on Sep 25, 2000 at 20:19 UTC
    First, welcome to the wonderful world of ClearCase administration. :-)

    Secondly, if you're using ClearCase on NT, you may want to shy away from using IPC::Open3 because it isn't included in Rational's slimmed down version of perl that comes bundled with ClearCase. Instead, I typically rely on $! and the error code. Basically, I know that the operation failed, and therefore I know what to do. In your example, if the checkout fails, your script can stop, give a warning, whatever. Typically, the reason why is in $!.

    You may want to register on Rational's Customer's only site. There you will find many contributed perl scripts which may help you in your endeavor.

    Don't forget, too, that there are some scripts in the ClearCase installation directory which also may help you get started.

    Cheers,
    Shendal