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

Hello monks. I have a brand new workhorse of a perl script to manage my backups. One small prob with it. I am using rsync at one point and so I am using backticks to call it. I suppose using system() or open() with |- might be more enlightened, if so I'd like your thoughts on that. But here is the code:
$| = 1; my $rsync_cmd = "rsync -vza --delete --delete-excluded $exclude_opts $ +conf{dir_backup_from} $mountpoint_reversed_enc"; print qx/$rsync_cmd/;
as you can see I am trying to use $! to get my qx// to flush rsync's output so I can see it scroll happily by. But it has no effect (of course). So is there any way to turn off the pesky buffering?

Replies are listed 'Best First'.
Re: backticks buffering, tally me banana
by ikegami (Patriarch) on Aug 27, 2008 at 03:57 UTC

    It's not so much a buffering issue as you waiting for the child to finish before printing anything. Just use system instead of backticks.

    my $rsync_cmd = "rsync -vza --delete --delete-excluded $exclude_opts $ +conf{dir_backup_from} $mountpoint_reversed_enc"; system($rsync_cmd);

    rsync is still free to buffer its output and there's nothing you can do about that, but since its STDOUT is connected to a terminal (assuming the Perl script's STDOUT is connected to a terminal), it'll probably avoid buffering its output.

    You have another unrelated problem if $conf{dir_backup_from} contains the path to a directory rather than bourne shell argument literal, one that can easily be fixed by using the multi-argument form of system.

    my @rsync_cmd = ( 'rsync', '-vza', '--delete', '--delete-excluded', split(' ', $exclude_opts), $conf{dir_backup_from}, $mountpoint_reversed_enc, ); system(@rsync_cmd);
Re: backticks buffering, tally me banana
by hangon (Deacon) on Aug 27, 2008 at 21:36 UTC
    backticks buffering, tally me banana

    Thanks. Now I can't get that damn song out of my head ;-}