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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|