http://qs321.pair.com?node_id=297738


in reply to File::Rsync output functions and global variables

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.

Replies are listed 'Best First'.
Re: Re: File::Rsync output functions and global variables
by sandfly (Beadle) on Oct 09, 2003 at 07:34 UTC
    > In most cases, you don't need to bother with this anyway.
    I mean you don't need to bother with the braces - you do have to test!