Home-built Twirling Bar Between Multiple System Calls

Discussion in 'Perl Misc' started by foolishbrat, May 6, 2007.

  1. foolishbrat

    foolishbrat Guest

    Dear all,
    AFAIK, progress/twirling bar can only be applied under loops. For
    example we can use Term::progressBar or Smart::Comments. But is there
    away to do it for system call like this:

    __BEGIN__
    #!/usr/bin/perl
    system("code1.out param1 > output.txt");
    system("code2.out param2 output.txt > output_final.txt");
    __END__

    How can I show progress/twirling Bar between first (code1) system call
    and the second (code2) system call? Especially we don't know how long
    each system call will take.

    Preferrably not using external CPAN module (except pre-installed
    module).
     
    foolishbrat, May 6, 2007
    #1
    1. Advertising

  2. On Sun, 06 May 2007 08:20:03 -0700, foolishbrat wrote:

    > Dear all,
    > AFAIK, progress/twirling bar can only be applied under loops. For
    > example we can use Term::progressBar or Smart::Comments. But is there
    > away to do it for system call like this:
    >
    > __BEGIN__
    > #!/usr/bin/perl
    > system("code1.out param1 > output.txt"); system("code2.out param2
    > output.txt > output_final.txt"); __END__
    >
    > How can I show progress/twirling Bar between first (code1) system call
    > and the second (code2) system call? Especially we don't know how long
    > each system call will take.
    >
    > Preferrably not using external CPAN module (except pre-installed
    > module).


    Easy. Fork to do the system call. In the parent show the progress bar and
    check with wait_pid if the child is ready.

    May or may not work on Windows, though, no idea really.

    HTH,
    M4
     
    Martijn Lievaart, May 6, 2007
    #2
    1. Advertising

  3. foolishbrat

    Joe Smith Guest

    Martijn Lievaart wrote:
    > On Sun, 06 May 2007 08:20:03 -0700, foolishbrat wrote:
    >> #!/usr/bin/perl
    >> system("code1.out param1 > output.txt");
    >> system("code2.out param2 output.txt > output_final.txt");
    >>
    >> How can I show progress/twirling Bar between first (code1) system call
    >> and the second (code2) system call? Especially we don't know how long
    >> each system call will take.

    >
    > Easy. Fork to do the system call. In the parent show the progress bar and
    > check with wait_pid if the child is ready.
    >
    > May or may not work on Windows, though, no idea really.


    #!/usr/bin/perl
    use strict; use warnings;
    $|++;

    our $child_pid; # Holds PID while child is running
    $SIG{CHLD} = sub { $child_pid = 0; }; # When child has exited
    # That works for POSIX-like systems (Linux, Cygwin), but not Windows-XP

    my $cmd = 'sleep 12'; # Example for code1.out
    if ($child_pid = fork) {
    print "Parent waiting for child $child_pid to finish: ";
    my $count;
    while ($child_pid) {
    printf "%s%4d\b\b\b\b\b", substr('-/|\\',++$count%4,1), $count;
    sleep 1;
    }
    print "Done!\n";
    } else {
    print "Child $$ running `$cmd`\n";
    exec $cmd; # code1.out runs here
    }
    system "echo code2.out runs here";
    print "Finished\n";

    -Joe
     
    Joe Smith, May 7, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    0
    Views:
    3,551
  2. John Hann
    Replies:
    0
    Views:
    6,085
    John Hann
    Aug 21, 2004
  3. Matt
    Replies:
    9
    Views:
    70,863
  4. Richard Tobin
    Replies:
    24
    Views:
    793
  5. Matt
    Replies:
    9
    Views:
    550
    kaeli
    Aug 23, 2004
Loading...

Share This Page