#! /usr/bin/perl

use strict;
use warnings;

use IO::Handle;
use Time::HiRes qw(usleep);

# try to open a progress bar with a pipe to it for i/o,
# $windowhandle is the handle for reading to/writing from it
open my $windowhandle, "|-",
   q{zenity --title=Working --width=400 --progress --percentage=50 --no-cancel --auto-kill --auto-close}
   or die "Could not open zenity: $!";

# make sure all output to the window is immediately flushed
$windowhandle->autoflush(1);

# go through 100 iterations, each 0.1 seconds long,
# updating the progress bar and the terminal window each time
for my $i ( 1.. 100 ) {
   usleep 100_000;                        # sleep for 0.1 seconds
   print $windowhandle "#Now on $i...\n"; # prints to the gui window
   print "We've done $i\n";               # prints to the command window
   if ($i > 50) {
      print $windowhandle "100\n";
   }
}
