robartes has asked for the wisdom of the Perl Monks concerning the following question:
The outfun and errfun options take a function reference. The function is called once for each line of output from the rsync program with the output line passed in as the first argument, the second arg is either 'out' or 'err' depending on the source. This makes it possible to use the same function for both and still determine where the output came from.
This is fine if you want to write the output to a log file or some such thing, but if you want to capture the output into a variable, you pretty much have to make it global. Something like:
package MyModule; use File::Rsync; my %rsync_out; my $rsync=File::Rsync->new( { outfun => &rsync_out, errfun => &rsync_out, } ); ... sub rsync_out { my ($message, $type)=shift; $rsync_out{$type}.="$message\n"; }
That should work, but it uses a global variable that is set somewhere downstream in the script in a subroutine, so it is somewhat ugly.
I have a sneaky feeling that something can be done here with references and closures, but I'm drawing a blank as to what exactly.
So, what's the monks' opinion: is this a tolerable use of a global variable or should I try to be clever and avoid the global (suggestions as to what exactly 'clever' means in this case are welcome :) )? Personally, I'm inclined to go with the global var, for the sake of simplicity and maintainability.
CU
Robartes-
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: File::Rsync output functions and global variables
by sandfly (Beadle) on Oct 08, 2003 at 21:07 UTC | |
by sandfly (Beadle) on Oct 09, 2003 at 07:34 UTC | |
|
Re: File::Rsync output functions and global variables
by robartes (Priest) on Oct 08, 2003 at 19:55 UTC | |
|
Re: File::Rsync output functions and global variables
by tilly (Archbishop) on Oct 12, 2003 at 02:44 UTC |