Syntactic Confectionery Delight | |
PerlMonks |
File::Rsync output functions and global variablesby robartes (Priest) |
on Oct 08, 2003 at 18:31 UTC ( [id://297686]=perlquestion: print w/replies, xml ) | Need Help?? |
robartes has asked for the wisdom of the Perl Monks concerning the following question:
I'm writing a module that uses File::Rsync, which has an option to capture the output from rsync through subrefs. From the docs:
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:
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
Back to
Seekers of Perl Wisdom
|
|