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

Hi Monks,
I am writing a script which copies the scr files from the ClearCase to local machine. I am trying to clearcase setview if it is not set already.
my $cmd= "/usr/atria/bin/cleartool"; my $test=system "$cmd setview $view"; ...... my $status= system("cp $file1 file2");
Problem here is, while setting up the view is running in background, the next command(cp) is executed and giving file not found error.
My requirement is, main program should wait till view is setup and then start the copy process.Please help me to implement this.
Thanks.

Replies are listed 'Best First'.
Re: ClearCase Programming
by jdporter (Paladin) on Jul 01, 2008 at 11:16 UTC

    The problem is not that the setview command hasn't completed, it's that it is running in a child process, and is not affecting the calling process at all. You need to arrange to have your perl script called from an environment where setview has already been done.

      I accept your view. But what i want is wait till the child process ends and then continue running the calling (parent process).
        Then you'll either want to use fork and waitpid or one of the many Fork modules on cpan that do that for you, such as Parallel::ForkManager.

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
        Using system() to execute the command does make the script wait until it's finished. Read it in the docs: system
Re: ClearCase Programming
by Arunbear (Prior) on Jul 01, 2008 at 16:53 UTC
    I used two scripts to try and simulate your problem:
    # write.pl my $pid = fork; if($pid == 0) { sleep 5; system('echo "hello world" > foo.txt'); }
    which creates the child worker process (I assumed you have no control over this process) and
    # copy.pl my $test = system('perl', 'write.pl'); my $file1 = 'foo.txt'; my $status = system("cp $file1 file2");
    Then running copy.pl produced:
    $ perl -w copy.pl cp: cannot stat `foo.txt': No such file or directory
    Then I changed copy.pl to copy2.pl
    # copy2.pl use strict; my $test = system('perl', 'write.pl'); my $file1 = 'foo.txt'; while(1) { last if -e $file1 && ! qx/lsof $file1/; } my $status = system("cp $file1 file2");
    which successfully copies the file. This method waits for the file to be created and waits until no other processes are using it before executing the copy command (and it assumes you have lsof available).
Re: ClearCase Programming
by almut (Canon) on Jul 01, 2008 at 17:28 UTC

    What about just executing both commands in a single system() command?

    my $cmd = "/usr/atria/bin/cleartool"; my $status = system("$cmd setview $view ; cp $file1 file2");

    In that case, both commands will be run by the same shell, and any environment set up by the first command will be available to the second. Also, the second command won't execute before the first has finished (as usual, shell commands separated by ';' are being executed in sequence — unless they fork themselves in the background (which I don't presume ct setview is doing...)).

    Or maybe

    my $status = system("$cmd setview $view && cp $file1 file2");

    in which case the second command will only execute if the first succeeded.

    (Essentially, you've then (dynamically) written a little shell script which you simply call from Perl.)

Re: ClearCase Programming
by NetWallah (Canon) on Jul 01, 2008 at 21:11 UTC
    To gain more (perl) control over the process, including the ability to parse the output of the cleartool commands, I recommend the following modules to perform the functions specified so far:

         Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax

Re: ClearCase Programming
by rovf (Priest) on Jul 02, 2008 at 08:13 UTC

    Would using the -exec option of setview help?

    -- 
    Ronald Fischer <ynnor@mm.st>