What you've done is perfectly acceptable. You have used 'my' to declare the output variable, so it isn't global, it's lexically scoped. In the example above, it is only visible to code within the file you declare it in.
If you need to limit the scope further, you can put braces around the code which needs to see it.
package MyModule;
use File::Rsync;
my $rsync;
{
my %rsync_out;
$rsync=File::Rsync->new( { outfun => &rsync_out,
errfun => &rsync_out,
} );
sub rsync_out {
my ($message, $type)=shift;
$rsync_out{$type}.="$message\n";
}
sub outmsg { $rsync_out{out} }
sub errmsg { $rsync_out{err} }
}
# ...
# code here has no clue about %rsync_out
# (but it can still see $rsync because I declared
# it outside the braces.)
# ...
# Access the output using the outmsg and errmsg subs
my $output = outmsg;
The code not tested. In most cases, you don't need to bother with this anyway.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.