Data-Trace

 view release on metacpan or  search on metacpan

lib/Data/Tie/Watch.pm  view on Meta::CPAN

package Data::Tie::Watch;

=head1 NAME

 Data::Tie::Watch - place watchpoints on Perl variables.

=cut

use 5.006;
use strict;
use warnings;
use Carp;
use Scalar::Util qw( reftype weaken );

our $VERSION = '1.302.2';
our %METHODS;

=head1 SYNOPSIS

 use Data::Tie::Watch;

 $watch = Data::Tie::Watch->new(
     -variable => \$frog,
     -shadow   => 0,			  
     -fetch    => [\&fetch, 'arg1', 'arg2', ..., 'argn'],
     -store    => \&store,
     -destroy  => sub {print "Final value=$frog.\n"},
 }
 $val   = $watch->Fetch;
 $watch->Store('Hello');
 $watch->Unwatch;

=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.

=cut

lib/Data/Tie/Watch.pm  view on Meta::CPAN

            -fetch   => \&Data::Tie::Watch::Scalar::Fetch,
            -store   => \&Data::Tie::Watch::Scalar::Store,
        );
    }
    elsif ( $type =~ /ARRAY/ ) {
        %methods = (
            -clear     => \&Data::Tie::Watch::Array::Clear,
            -delete    => \&Data::Tie::Watch::Array::Delete,
            -destroy   => \&Data::Tie::Watch::Array::Destroy,
            -exists    => \&Data::Tie::Watch::Array::Exists,
            -extend    => \&Data::Tie::Watch::Array::Extend,
            -fetch     => \&Data::Tie::Watch::Array::Fetch,
            -fetchsize => \&Data::Tie::Watch::Array::Fetchsize,
            -pop       => \&Data::Tie::Watch::Array::Pop,
            -push      => \&Data::Tie::Watch::Array::Push,
            -shift     => \&Data::Tie::Watch::Array::Shift,
            -splice    => \&Data::Tie::Watch::Array::Splice,
            -store     => \&Data::Tie::Watch::Array::Store,
            -storesize => \&Data::Tie::Watch::Array::Storesize,
            -unshift   => \&Data::Tie::Watch::Array::Unshift,
        );
    }
    elsif ( $type =~ /HASH/ ) {
        %methods = (
            -clear    => \&Data::Tie::Watch::Hash::Clear,
            -delete   => \&Data::Tie::Watch::Hash::Delete,
            -destroy  => \&Data::Tie::Watch::Hash::Destroy,
            -exists   => \&Data::Tie::Watch::Hash::Exists,
            -fetch    => \&Data::Tie::Watch::Hash::Fetch,
            -firstkey => \&Data::Tie::Watch::Hash::Firstkey,
            -nextkey  => \&Data::Tie::Watch::Hash::Nextkey,
            -store    => \&Data::Tie::Watch::Hash::Store,
        );
    }
    else {
        croak "Data::Tie::Watch::new() - not a variable reference.";
    }

    \%methods;
}

sub _build_obj {
    my ( $class, %args ) = @_;
    my $var  = $args{-variable};
    my $type = reftype( $var ) // '';
    my $watch_obj;

    if ( $type =~ /(SCALAR|REF)/ ) {
        $watch_obj = tie $$var, 'Data::Tie::Watch::Scalar', %args;
    }
    elsif ( $type =~ /ARRAY/ ) {
        $watch_obj = tie @$var, 'Data::Tie::Watch::Array', %args;
    }
    elsif ( $type =~ /HASH/ ) {
        $watch_obj = tie %$var, 'Data::Tie::Watch::Hash', %args;
    }

    $watch_obj->{id}   = "$watch_obj";
    $watch_obj->{type} = $type;

    # weaken $watch_obj->{-variable};

    $watch_obj;
}

=head2 DESTROY

Clean up global cache.

Note: Originally the 'Unwatch()' method call was placed at just before the
return of 'callback()' which appeared to be the logical place for it.
However it would occasionally provoke a segmentation fault (possibly
indirectly).

=cut

sub DESTROY {
    $_[0]->callback( '-destroy' );
    $_[0]->Unwatch();
    delete $METHODS{"$_[0]"};
}

=head2 Unwatch

Stop watching a variable by releasing the last
reference and untieing it.

Updates the original variable with its shadow,
if appropriate.

=cut

sub Unwatch {
    my $var = $_[0]->{-variable};
    return if not $var;

    my $type = reftype( $var ) // '';
    return if not $type;

    my $copy;
    $copy = $_[0]->{-ptr} if $type !~ /(SCALAR|REF)/;
    my $shadow = $_[0]->{-shadow};
    undef $_[0];

    if ( $type =~ /(SCALAR|REF)/ ) {
        untie $$var;
    }
    elsif ( $type =~ /ARRAY/ ) {
        untie @$var;
        @$var = @$copy if $shadow && $copy;
    }
    elsif ( $type =~ /HASH/ ) {
        untie %$var;
        %$var = %$copy if $shadow;
    }
    else {
        croak "not a variable reference.";
    }
}

=head2 base_watch



( run in 0.522 second using v1.01-cache-2.11-cpan-39bf76dae61 )