Data-Trace
view release on metacpan or search on metacpan
lib/Data/Tie/Watch.pm view on Meta::CPAN
=cut
=head1 DESCRIPTION
Note: This is a copy of Tk's Tie::Watch.
Copied to avoid the Tk depedency.
This class module binds one or more subroutines of your devising to a
Perl variable. All variables can have B<FETCH>, B<STORE> and
B<DESTROY> callbacks. Additionally, arrays can define B<CLEAR>,
B<DELETE>, B<EXISTS>, B<EXTEND>, B<FETCHSIZE>, B<POP>, B<PUSH>,
B<SHIFT>, B<SPLICE>, B<STORESIZE> and B<UNSHIFT> callbacks, and hashes
can define B<CLEAR>, B<DELETE>, B<EXISTS>, B<FIRSTKEY> and B<NEXTKEY>
callbacks. If these term are unfamiliar to you, I I<really> suggest
you read L<perltie>.
With Data::Tie::Watch you can:
. alter a variable's value
. prevent a variable's value from being changed
. invoke a Perl/Tk callback when a variable changes
. trace references to a variable
Callback format is patterned after the Perl/Tk scheme: supply either a
code reference, or, supply an array reference and pass the callback
code reference in the first element of the array, followed by callback
arguments. (See examples in the Synopsis, above.)
Tie::Watch provides default callbacks for any that you fail to
specify. Other than negatively impacting performance, they perform
the standard action that you'd expect, so the variable behaves
"normally". Once you override a default callback, perhaps to insert
debug code like print statements, your callback normally finishes by
calling the underlying (overridden) method. But you don't have to!
To map a tied method name to a default callback name simply lowercase
the tied method name and uppercase its first character. So FETCH
becomes Fetch, NEXTKEY becomes Nextkey, etcetera.
lib/Data/Tie/Watch.pm view on Meta::CPAN
=cut
=head2 new
Watch constructor.
The *real* constructor is Data::Tie::Watch->base_watch(),
invoked by methods in other Watch packages, depending upon the variable's
type. Here we supply defaulted parameter values and then verify them,
normalize all callbacks and bind the variable to the appropriate package.
The watchpoint constructor method that accepts option/value pairs to
create and configure the Watch object. The only required option is
B<-variable>.
B<-variable> is a I<reference> to a scalar, array or hash variable.
B<-shadow> (default 1) is 0 to disable array and hash shadowing. To
prevent infinite recursion Data::Tie::Watch maintains parallel variables for
arrays and hashes. When the watchpoint is created the parallel shadow
lib/Data/Tie/Watch.pm view on Meta::CPAN
sub TIESCALAR {
my ( $class, %args ) = @_;
my $variable = $args{-variable};
my $watch_obj = Data::Tie::Watch->base_watch( %args );
$watch_obj->{-value} = $$variable;
bless $watch_obj, $class;
}
# Default scalar callbacks.
sub Destroy { undef %{ $_[0] } }
sub Fetch { $_[0]->{-value} }
sub Store { $_[0]->{-value} = $_[1] }
# Scalar access methods.
sub FETCH { $_[0]->callback( '-fetch' ) }
sub STORE { $_[0]->callback( '-store', $_[1] ) }
lib/Data/Tie/Watch.pm view on Meta::CPAN
my ( $class, %args ) = @_;
my ( $variable, $shadow ) = @args{ -variable, -shadow };
my @copy;
@copy = @$variable if $shadow; # make a private copy of user's array
$args{-ptr} = $shadow ? \@copy : [];
my $watch_obj = Data::Tie::Watch->base_watch( %args );
bless $watch_obj, $class;
}
# Default array callbacks.
sub Clear { $_[0]->{-ptr} = () }
sub Delete { delete $_[0]->{-ptr}->[ $_[1] ] }
sub Destroy { undef %{ $_[0] } }
sub Exists { exists $_[0]->{-ptr}->[ $_[1] ] }
sub Extend { }
sub Fetch { $_[0]->{-ptr}->[ $_[1] ] }
sub Fetchsize { scalar @{ $_[0]->{-ptr} // [] } }
sub Pop { pop @{ $_[0]->{-ptr} } }
sub Push { push @{ $_[0]->{-ptr} }, @_[ 1 .. $#_ ] }
lib/Data/Tie/Watch.pm view on Meta::CPAN
my ( $class, %args ) = @_;
my ( $variable, $shadow ) = @args{ -variable, -shadow };
my %copy;
%copy = %$variable if $shadow; # make a private copy of user's hash
$args{-ptr} = $shadow ? \%copy : {};
my $watch_obj = Data::Tie::Watch->base_watch( %args );
bless $watch_obj, $class;
}
# Default hash callbacks.
sub Clear { $_[0]->{-ptr} = () }
sub Delete { delete $_[0]->{-ptr}->{ $_[1] } }
sub Destroy { undef %{ $_[0] } }
sub Exists { exists $_[0]->{-ptr}->{ $_[1] } }
sub Fetch { $_[0]->{-ptr}->{ $_[1] } }
sub Firstkey { my $c = keys %{ $_[0]->{-ptr} }; each %{ $_[0]->{-ptr} } }
sub Nextkey { each %{ $_[0]->{-ptr} } }
sub Store { $_[0]->{-ptr}->{ $_[1] } = $_[2] }
( run in 0.406 second using v1.01-cache-2.11-cpan-9b1e4054eb1 )