| 1 |
$Tie::Watch::VERSION = '1.302'; |
| 2 |
|
| 3 |
package Tie::Watch; |
| 4 |
|
| 5 |
=head1 NAME |
| 6 |
|
| 7 |
Tie::Watch - place watchpoints on Perl variables. |
| 8 |
|
| 9 |
=head1 SYNOPSIS |
| 10 |
|
| 11 |
use Tie::Watch; |
| 12 |
|
| 13 |
$watch = Tie::Watch->new( |
| 14 |
-variable => \$frog, |
| 15 |
-debug => 1, |
| 16 |
-shadow => 0, |
| 17 |
-fetch => [\&fetch, 'arg1', 'arg2', ..., 'argn'], |
| 18 |
-store => \&store, |
| 19 |
-destroy => sub {print "Final value=$frog.\n"}, |
| 20 |
} |
| 21 |
%vinfo = $watch->Info; |
| 22 |
$args = $watch->Args(-fetch); |
| 23 |
$val = $watch->Fetch; |
| 24 |
print "val=", $watch->Say($val), ".\n"; |
| 25 |
$watch->Store('Hello'); |
| 26 |
$watch->Unwatch; |
| 27 |
|
| 28 |
=head1 DESCRIPTION |
| 29 |
|
| 30 |
This class module binds one or more subroutines of your devising to a |
| 31 |
Perl variable. All variables can have B<FETCH>, B<STORE> and |
| 32 |
B<DESTROY> callbacks. Additionally, arrays can define B<CLEAR>, |
| 33 |
B<DELETE>, B<EXISTS>, B<EXTEND>, B<FETCHSIZE>, B<POP>, B<PUSH>, |
| 34 |
B<SHIFT>, B<SPLICE>, B<STORESIZE> and B<UNSHIFT> callbacks, and hashes |
| 35 |
can define B<CLEAR>, B<DELETE>, B<EXISTS>, B<FIRSTKEY> and B<NEXTKEY> |
| 36 |
callbacks. If these term are unfamiliar to you, I I<really> suggest |
| 37 |
you read L<perltie>. |
| 38 |
|
| 39 |
With Tie::Watch you can: |
| 40 |
|
| 41 |
. alter a variable's value |
| 42 |
. prevent a variable's value from being changed |
| 43 |
. invoke a Perl/Tk callback when a variable changes |
| 44 |
. trace references to a variable |
| 45 |
|
| 46 |
Callback format is patterned after the Perl/Tk scheme: supply either a |
| 47 |
code reference, or, supply an array reference and pass the callback |
| 48 |
code reference in the first element of the array, followed by callback |
| 49 |
arguments. (See examples in the Synopsis, above.) |
| 50 |
|
| 51 |
Tie::Watch provides default callbacks for any that you fail to |
| 52 |
specify. Other than negatively impacting performance, they perform |
| 53 |
the standard action that you'd expect, so the variable behaves |
| 54 |
"normally". Once you override a default callback, perhaps to insert |
| 55 |
debug code like print statements, your callback normally finishes by |
| 56 |
calling the underlying (overridden) method. But you don't have to! |
| 57 |
|
| 58 |
To map a tied method name to a default callback name simply lowercase |
| 59 |
the tied method name and uppercase its first character. So FETCH |
| 60 |
becomes Fetch, NEXTKEY becomes Nextkey, etcetera. |
| 61 |
|
| 62 |
Here are two callbacks for a scalar. The B<FETCH> (read) callback does |
| 63 |
nothing other than illustrate the fact that it returns the value to |
| 64 |
assign the variable. The B<STORE> (write) callback uppercases the |
| 65 |
variable and returns it. In all cases the callback I<must> return the |
| 66 |
correct read or write value - typically, it does this by invoking the |
| 67 |
underlying method. |
| 68 |
|
| 69 |
my $fetch_scalar = sub { |
| 70 |
my($self) = @_; |
| 71 |
$self->Fetch; |
| 72 |
}; |
| 73 |
|
| 74 |
my $store_scalar = sub { |
| 75 |
my($self, $new_val) = @_; |
| 76 |
$self->Store(uc $new_val); |
| 77 |
}; |
| 78 |
|
| 79 |
Here are B<FETCH> and B<STORE> callbacks for either an array or hash. |
| 80 |
They do essentially the same thing as the scalar callbacks, but |
| 81 |
provide a little more information. |
| 82 |
|
| 83 |
my $fetch = sub { |
| 84 |
my($self, $key) = @_; |
| 85 |
my $val = $self->Fetch($key); |
| 86 |
print "In fetch callback, key=$key, val=", $self->Say($val); |
| 87 |
my $args = $self->Args(-fetch); |
| 88 |
print ", args=('", join("', '", @$args), "')" if $args; |
| 89 |
print ".\n"; |
| 90 |
$val; |
| 91 |
}; |
| 92 |
|
| 93 |
my $store = sub { |
| 94 |
my($self, $key, $new_val) = @_; |
| 95 |
my $val = $self->Fetch($key); |
| 96 |
$new_val = uc $new_val; |
| 97 |
$self->Store($key, $new_val); |
| 98 |
print "In store callback, key=$key, val=", $self->Say($val), |
| 99 |
", new_val=", $self->Say($new_val); |
| 100 |
my $args = $self->Args(-store); |
| 101 |
print ", args=('", join("', '", @$args), "')" if $args; |
| 102 |
print ".\n"; |
| 103 |
$new_val; |
| 104 |
}; |
| 105 |
|
| 106 |
In all cases, the first parameter is a reference to the Watch object, |
| 107 |
used to invoke the following class methods. |
| 108 |
|
| 109 |
=head1 METHODS |
| 110 |
|
| 111 |
=over 4 |
| 112 |
|
| 113 |
=item $watch = Tie::Watch->new(-options => values); |
| 114 |
|
| 115 |
The watchpoint constructor method that accepts option/value pairs to |
| 116 |
create and configure the Watch object. The only required option is |
| 117 |
B<-variable>. |
| 118 |
|
| 119 |
B<-variable> is a I<reference> to a scalar, array or hash variable. |
| 120 |
|
| 121 |
B<-debug> (default 0) is 1 to activate debug print statements internal |
| 122 |
to Tie::Watch. |
| 123 |
|
| 124 |
B<-shadow> (default 1) is 0 to disable array and hash shadowing. To |
| 125 |
prevent infinite recursion Tie::Watch maintains parallel variables for |
| 126 |
arrays and hashes. When the watchpoint is created the parallel shadow |
| 127 |
variable is initialized with the watched variable's contents, and when |
| 128 |
the watchpoint is deleted the shadow variable is copied to the original |
| 129 |
variable. Thus, changes made during the watch process are not lost. |
| 130 |
Shadowing is on my default. If you disable shadowing any changes made |
| 131 |
to an array or hash are lost when the watchpoint is deleted. |
| 132 |
|
| 133 |
Specify any of the following relevant callback parameters, in the |
| 134 |
format described above: B<-fetch>, B<-store>, B<-destroy>. |
| 135 |
Additionally for arrays: B<-clear>, B<-extend>, B<-fetchsize>, |
| 136 |
B<-pop>, B<-push>, B<-shift>, B<-splice>, B<-storesize> and |
| 137 |
B<-unshift>. Additionally for hashes: B<-clear>, B<-delete>, |
| 138 |
B<-exists>, B<-firstkey> and B<-nextkey>. |
| 139 |
|
| 140 |
=item $args = $watch->Args(-fetch); |
| 141 |
|
| 142 |
Returns a reference to a list of arguments for the specified callback, |
| 143 |
or undefined if none. |
| 144 |
|
| 145 |
=item $watch->Fetch(); $watch->Fetch($key); |
| 146 |
|
| 147 |
Returns a variable's current value. $key is required for an array or |
| 148 |
hash. |
| 149 |
|
| 150 |
=item %vinfo = $watch->Info(); |
| 151 |
|
| 152 |
Returns a hash detailing the internals of the Watch object, with these |
| 153 |
keys: |
| 154 |
|
| 155 |
%vinfo = { |
| 156 |
-variable => SCALAR(0x200737f8) |
| 157 |
-debug => '0' |
| 158 |
-shadow => '1' |
| 159 |
-value => 'HELLO SCALAR' |
| 160 |
-destroy => ARRAY(0x200f86cc) |
| 161 |
-fetch => ARRAY(0x200f8558) |
| 162 |
-store => ARRAY(0x200f85a0) |
| 163 |
-legible => above data formatted as a list of string, for printing |
| 164 |
} |
| 165 |
|
| 166 |
For array and hash Watch objects, the B<-value> key is replaced with a |
| 167 |
B<-ptr> key which is a reference to the parallel array or hash. |
| 168 |
Additionally, for an array or hash, there are key/value pairs for |
| 169 |
all the variable specific callbacks. |
| 170 |
|
| 171 |
=item $watch->Say($val); |
| 172 |
|
| 173 |
Used mainly for debugging, it returns $val in quotes if required, or |
| 174 |
the string "undefined" for undefined values. |
| 175 |
|
| 176 |
=item $watch->Store($new_val); $watch->Store($key, $new_val); |
| 177 |
|
| 178 |
Store a variable's new value. $key is required for an array or hash. |
| 179 |
|
| 180 |
=item $watch->Unwatch(); |
| 181 |
|
| 182 |
Stop watching the variable. |
| 183 |
|
| 184 |
=back |
| 185 |
|
| 186 |
=head1 EFFICIENCY CONSIDERATIONS |
| 187 |
|
| 188 |
If you can live using the class methods provided, please do so. You |
| 189 |
can meddle with the object hash directly and improved watch |
| 190 |
performance, at the risk of your code breaking in the future. |
| 191 |
|
| 192 |
=head1 AUTHOR |
| 193 |
|
| 194 |
Stephen O. Lidie |
| 195 |
|
| 196 |
=head1 HISTORY |
| 197 |
|
| 198 |
[email protected], LUCC, 96/05/30 |
| 199 |
. Original version 0.92 release, based on the Trace module from Hans Mulder, |
| 200 |
and ideas from Tim Bunce. |
| 201 |
|
| 202 |
[email protected], LUCC, 96/12/25 |
| 203 |
. Version 0.96, release two inner references detected by Perl 5.004. |
| 204 |
|
| 205 |
[email protected], LUCC, 97/01/11 |
| 206 |
. Version 0.97, fix Makefile.PL and MANIFEST (thanks Andreas Koenig). |
| 207 |
Make sure test.pl doesn't fail if Tk isn't installed. |
| 208 |
|
| 209 |
[email protected], Lehigh University Computing Center, 97/10/03 |
| 210 |
. Version 0.98, implement -shadow option for arrays and hashes. |
| 211 |
|
| 212 |
[email protected], Lehigh University Computing Center, 98/02/11 |
| 213 |
. Version 0.99, finally, with Perl 5.004_57, we can completely watch arrays. |
| 214 |
With tied array support this module is essentially complete, so its been |
| 215 |
optimized for speed at the expense of clarity - sorry about that. The |
| 216 |
Delete() method has been renamed Unwatch() because it conflicts with the |
| 217 |
builtin delete(). |
| 218 |
|
| 219 |
[email protected], Lehigh University Computing Center, 99/04/04 |
| 220 |
. Version 1.0, for Perl 5.005_03, update Makefile.PL for ActiveState, and |
| 221 |
add two examples (one for Perl/Tk). |
| 222 |
|
| 223 |
[email protected], Lehigh University Computing Center, 2003/06/07 |
| 224 |
. Version 1.1, for Perl 5.8, can trace a reference now, patch from Slaven |
| 225 |
Rezic. |
| 226 |
|
| 227 |
[email protected], Lehigh University Computing Center, 2005/05/17 |
| 228 |
. Version 1.2, for Perl 5.8, per Rob Seegel's suggestion, support array |
| 229 |
DELETE and EXISTS. |
| 230 |
|
| 231 |
=head1 COPYRIGHT |
| 232 |
|
| 233 |
Copyright (C) 1996 - 2005 Stephen O. Lidie. All rights reserved. |
| 234 |
|
| 235 |
This program is free software; you can redistribute it and/or modify it under |
| 236 |
the same terms as Perl itself. |
| 237 |
|
| 238 |
=cut |
| 239 |
|
| 240 |
use 5.004_57;; |
| 241 |
use Carp; |
| 242 |
use strict; |
| 243 |
use subs qw/normalize_callbacks/; |
| 244 |
use vars qw/@array_callbacks @hash_callbacks @scalar_callbacks/; |
| 245 |
|
| 246 |
@array_callbacks = qw/-clear -delete -destroy -exists -extend -fetch |
| 247 |
-fetchsize -pop -push -shift -splice -store |
| 248 |
-storesize -unshift/; |
| 249 |
@hash_callbacks = qw/-clear -delete -destroy -exists -fetch -firstkey |
| 250 |
-nextkey -store/; |
| 251 |
@scalar_callbacks = qw/-destroy -fetch -store/; |
| 252 |
|
| 253 |
sub new { |
| 254 |
|
| 255 |
# Watch constructor. The *real* constructor is Tie::Watch->base_watch(), |
| 256 |
# invoked by methods in other Watch packages, depending upon the variable's |
| 257 |
# type. Here we supply defaulted parameter values and then verify them, |
| 258 |
# normalize all callbacks and bind the variable to the appropriate package. |
| 259 |
|
| 260 |
my($class, %args) = @_; |
| 261 |
my $version = $Tie::Watch::VERSION; |
| 262 |
my (%arg_defaults) = (-debug => 0, -shadow => 1); |
| 263 |
my $variable = $args{-variable}; |
| 264 |
croak "Tie::Watch::new(): -variable is required." if not defined $variable; |
| 265 |
|
| 266 |
my($type, $watch_obj) = (ref $variable, undef); |
| 267 |
if ($type =~ /(SCALAR|REF)/) { |
| 268 |
@arg_defaults{@scalar_callbacks} = ( |
| 269 |
[\&Tie::Watch::Scalar::Destroy], [\&Tie::Watch::Scalar::Fetch], |
| 270 |
[\&Tie::Watch::Scalar::Store]); |
| 271 |
} elsif ($type =~ /ARRAY/) { |
| 272 |
@arg_defaults{@array_callbacks} = ( |
| 273 |
[\&Tie::Watch::Array::Clear], [\&Tie::Watch::Array::Delete], |
| 274 |
[\&Tie::Watch::Array::Destroy], [\&Tie::Watch::Array::Exists], |
| 275 |
[\&Tie::Watch::Array::Extend], [\&Tie::Watch::Array::Fetch], |
| 276 |
[\&Tie::Watch::Array::Fetchsize], [\&Tie::Watch::Array::Pop], |
| 277 |
[\&Tie::Watch::Array::Push], [\&Tie::Watch::Array::Shift], |
| 278 |
[\&Tie::Watch::Array::Splice], [\&Tie::Watch::Array::Store], |
| 279 |
[\&Tie::Watch::Array::Storesize], [\&Tie::Watch::Array::Unshift]); |
| 280 |
} elsif ($type =~ /HASH/) { |
| 281 |
@arg_defaults{@hash_callbacks} = ( |
| 282 |
[\&Tie::Watch::Hash::Clear], [\&Tie::Watch::Hash::Delete], |
| 283 |
[\&Tie::Watch::Hash::Destroy], [\&Tie::Watch::Hash::Exists], |
| 284 |
[\&Tie::Watch::Hash::Fetch], [\&Tie::Watch::Hash::Firstkey], |
| 285 |
[\&Tie::Watch::Hash::Nextkey], [\&Tie::Watch::Hash::Store]); |
| 286 |
} else { |
| 287 |
croak "Tie::Watch::new() - not a variable reference."; |
| 288 |
} |
| 289 |
my(@margs, %ahsh, $args, @args); |
| 290 |
@margs = grep ! defined $args{$_}, keys %arg_defaults; |
| 291 |
%ahsh = %args; # argument hash |
| 292 |
@ahsh{@margs} = @arg_defaults{@margs}; # fill in missing values |
| 293 |
normalize_callbacks \%ahsh; |
| 294 |
|
| 295 |
if ($type =~ /(SCALAR|REF)/) { |
| 296 |
$watch_obj = tie $$variable, 'Tie::Watch::Scalar', %ahsh; |
| 297 |
} elsif ($type =~ /ARRAY/) { |
| 298 |
$watch_obj = tie @$variable, 'Tie::Watch::Array', %ahsh; |
| 299 |
} elsif ($type =~ /HASH/) { |
| 300 |
$watch_obj = tie %$variable, 'Tie::Watch::Hash', %ahsh; |
| 301 |
} |
| 302 |
$watch_obj; |
| 303 |
|
| 304 |
} # end new, Watch constructor |
| 305 |
|
| 306 |
sub Args { |
| 307 |
|
| 308 |
# Return a reference to a list of callback arguments, or undef if none. |
| 309 |
# |
| 310 |
# $_[0] = self |
| 311 |
# $_[1] = callback type |
| 312 |
|
| 313 |
defined $_[0]->{$_[1]}->[1] ? [@{$_[0]->{$_[1]}}[1 .. $#{$_[0]->{$_[1]}}]] |
| 314 |
: undef; |
| 315 |
|
| 316 |
} # end Args |
| 317 |
|
| 318 |
sub Info { |
| 319 |
|
| 320 |
# Info() method subclassed by other Watch modules. |
| 321 |
# |
| 322 |
# $_[0] = self |
| 323 |
# @_[1 .. $#_] = optional callback types |
| 324 |
|
| 325 |
my(%vinfo, @results); |
| 326 |
my(@info) = (qw/-variable -debug -shadow/); |
| 327 |
push @info, @_[1 .. $#_] if scalar @_ >= 2; |
| 328 |
foreach my $type (@info) { |
| 329 |
push @results, sprintf('%-10s: ', substr $type, 1) . |
| 330 |
$_[0]->Say($_[0]->{$type}); |
| 331 |
$vinfo{$type} = $_[0]->{$type}; |
| 332 |
} |
| 333 |
$vinfo{-legible} = [@results]; |
| 334 |
%vinfo; |
| 335 |
|
| 336 |
} # end Info |
| 337 |
|
| 338 |
sub Say { |
| 339 |
|
| 340 |
# For debugging, mainly. |
| 341 |
# |
| 342 |
# $_[0] = self |
| 343 |
# $_[1] = value |
| 344 |
|
| 345 |
defined $_[1] ? (ref($_[1]) ne '' ? $_[1] : "'$_[1]'") : "undefined"; |
| 346 |
|
| 347 |
} # end Say |
| 348 |
|
| 349 |
sub Unwatch { |
| 350 |
|
| 351 |
# Stop watching a variable by releasing the last reference and untieing it. |
| 352 |
# Update the original variable with its shadow, if appropriate. |
| 353 |
# |
| 354 |
# $_[0] = self |
| 355 |
|
| 356 |
my $variable = $_[0]->{-variable}; |
| 357 |
my $type = ref $variable; |
| 358 |
my $copy; $copy = $_[0]->{-ptr} if $type !~ /(SCALAR|REF)/; |
| 359 |
my $shadow = $_[0]->{-shadow}; |
| 360 |
undef $_[0]; |
| 361 |
if ($type =~ /(SCALAR|REF)/) { |
| 362 |
untie $$variable; |
| 363 |
} elsif ($type =~ /ARRAY/) { |
| 364 |
untie @$variable; |
| 365 |
@$variable = @$copy if $shadow; |
| 366 |
} elsif ($type =~ /HASH/) { |
| 367 |
untie %$variable; |
| 368 |
%$variable = %$copy if $shadow; |
| 369 |
} else { |
| 370 |
croak "Tie::Watch::Delete() - not a variable reference."; |
| 371 |
} |
| 372 |
|
| 373 |
} # end Unwatch |
| 374 |
|
| 375 |
# Watch private methods. |
| 376 |
|
| 377 |
sub base_watch { |
| 378 |
|
| 379 |
# Watch base class constructor invoked by other Watch modules. |
| 380 |
|
| 381 |
my($class, %args) = @_; |
| 382 |
my $watch_obj = {%args}; |
| 383 |
$watch_obj; |
| 384 |
|
| 385 |
} # end base_watch |
| 386 |
|
| 387 |
sub callback { |
| 388 |
|
| 389 |
# Execute a Watch callback, either the default or user specified. |
| 390 |
# Note that the arguments are those supplied by the tied method, |
| 391 |
# not those (if any) specified by the user when the watch object |
| 392 |
# was instantiated. This is for performance reasons, and why the |
| 393 |
# Args() method exists. |
| 394 |
# |
| 395 |
# $_[0] = self |
| 396 |
# $_[1] = callback type |
| 397 |
# $_[2] through $#_ = tied arguments |
| 398 |
|
| 399 |
&{$_[0]->{$_[1]}->[0]} ($_[0], @_[2 .. $#_]); |
| 400 |
|
| 401 |
} # end callback |
| 402 |
|
| 403 |
sub normalize_callbacks { |
| 404 |
|
| 405 |
# Ensure all callbacks are normalized in [\&code, @args] format. |
| 406 |
|
| 407 |
my($args_ref) = @_; |
| 408 |
my($cb, $ref); |
| 409 |
foreach my $arg (keys %$args_ref) { |
| 410 |
next if $arg =~ /variable|debug|shadow/; |
| 411 |
$cb = $args_ref->{$arg}; |
| 412 |
$ref = ref $cb; |
| 413 |
if ($ref =~ /CODE/) { |
| 414 |
$args_ref->{$arg} = [$cb]; |
| 415 |
} elsif ($ref !~ /ARRAY/) { |
| 416 |
croak "Tie::Watch: malformed callback $arg=$cb."; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
} # end normalize_callbacks |
| 421 |
|
| 422 |
############################################################################### |
| 423 |
|
| 424 |
package # temporarily disabled from PAUSE indexer because of permission problems |
| 425 |
Tie::Watch::Scalar; |
| 426 |
|
| 427 |
use Carp; |
| 428 |
@Tie::Watch::Scalar::ISA = qw/Tie::Watch/; |
| 429 |
|
| 430 |
sub TIESCALAR { |
| 431 |
|
| 432 |
my($class, %args) = @_; |
| 433 |
my $variable = $args{-variable}; |
| 434 |
my $watch_obj = Tie::Watch->base_watch(%args); |
| 435 |
$watch_obj->{-value} = $$variable; |
| 436 |
print "WatchScalar new: $variable created, \@_=", join(',', @_), "!\n" |
| 437 |
if $watch_obj->{-debug}; |
| 438 |
bless $watch_obj, $class; |
| 439 |
|
| 440 |
} # end TIESCALAR |
| 441 |
|
| 442 |
sub Info {$_[0]->SUPER::Info('-value', @Tie::Watch::scalar_callbacks)} |
| 443 |
|
| 444 |
# Default scalar callbacks. |
| 445 |
|
| 446 |
sub Destroy {undef %{$_[0]}} |
| 447 |
sub Fetch {$_[0]->{-value}} |
| 448 |
sub Store {$_[0]->{-value} = $_[1]} |
| 449 |
|
| 450 |
# Scalar access methods. |
| 451 |
|
| 452 |
sub DESTROY {$_[0]->callback('-destroy')} |
| 453 |
sub FETCH {$_[0]->callback('-fetch')} |
| 454 |
sub STORE {$_[0]->callback('-store', $_[1])} |
| 455 |
|
| 456 |
############################################################################### |
| 457 |
|
| 458 |
package # temporarily disabled from PAUSE indexer because of permission problems |
| 459 |
Tie::Watch::Array; |
| 460 |
|
| 461 |
use Carp; |
| 462 |
@Tie::Watch::Array::ISA = qw/Tie::Watch/; |
| 463 |
|
| 464 |
sub TIEARRAY { |
| 465 |
|
| 466 |
my($class, %args) = @_; |
| 467 |
my($variable, $shadow) = @args{-variable, -shadow}; |
| 468 |
my @copy; @copy = @$variable if $shadow; # make a private copy of user's array |
| 469 |
$args{-ptr} = $shadow ? \@copy : []; |
| 470 |
my $watch_obj = Tie::Watch->base_watch(%args); |
| 471 |
print "WatchArray new: $variable created, \@_=", join(',', @_), "!\n" |
| 472 |
if $watch_obj->{-debug}; |
| 473 |
bless $watch_obj, $class; |
| 474 |
|
| 475 |
} # end TIEARRAY |
| 476 |
|
| 477 |
sub Info {$_[0]->SUPER::Info('-ptr', @Tie::Watch::array_callbacks)} |
| 478 |
|
| 479 |
# Default array callbacks. |
| 480 |
|
| 481 |
sub Clear {$_[0]->{-ptr} = ()} |
| 482 |
sub Delete {delete $_[0]->{-ptr}->[$_[1]]} |
| 483 |
sub Destroy {undef %{$_[0]}} |
| 484 |
sub Exists {exists $_[0]->{-ptr}->[$_[1]]} |
| 485 |
sub Extend {} |
| 486 |
sub Fetch {$_[0]->{-ptr}->[$_[1]]} |
| 487 |
sub Fetchsize {scalar @{$_[0]->{-ptr}}} |
| 488 |
sub Pop {pop @{$_[0]->{-ptr}}} |
| 489 |
sub Push {push @{$_[0]->{-ptr}}, @_[1 .. $#_]} |
| 490 |
sub Shift {shift @{$_[0]->{-ptr}}} |
| 491 |
sub Splice { |
| 492 |
my $n = scalar @_; # splice() is wierd! |
| 493 |
return splice @{$_[0]->{-ptr}}, $_[1] if $n == 2; |
| 494 |
return splice @{$_[0]->{-ptr}}, $_[1], $_[2] if $n == 3; |
| 495 |
return splice @{$_[0]->{-ptr}}, $_[1], $_[2], @_[3 .. $#_] if $n >= 4; |
| 496 |
} |
| 497 |
sub Store {$_[0]->{-ptr}->[$_[1]] = $_[2]} |
| 498 |
sub Storesize {$#{$_[0]->{-ptr}} = $_[1] - 1} |
| 499 |
sub Unshift {unshift @{$_[0]->{-ptr}}, @_[1 .. $#_]} |
| 500 |
|
| 501 |
# Array access methods. |
| 502 |
|
| 503 |
sub CLEAR {$_[0]->callback('-clear')} |
| 504 |
sub DELETE {$_[0]->callback('-delete', $_[1])} |
| 505 |
sub DESTROY {$_[0]->callback('-destroy')} |
| 506 |
sub EXISTS {$_[0]->callback('-exists', $_[1])} |
| 507 |
sub EXTEND {$_[0]->callback('-extend', $_[1])} |
| 508 |
sub FETCH {$_[0]->callback('-fetch', $_[1])} |
| 509 |
sub FETCHSIZE {$_[0]->callback('-fetchsize')} |
| 510 |
sub POP {$_[0]->callback('-pop')} |
| 511 |
sub PUSH {$_[0]->callback('-push', @_[1 .. $#_])} |
| 512 |
sub SHIFT {$_[0]->callback('-shift')} |
| 513 |
sub SPLICE {$_[0]->callback('-splice', @_[1 .. $#_])} |
| 514 |
sub STORE {$_[0]->callback('-store', $_[1], $_[2])} |
| 515 |
sub STORESIZE {$_[0]->callback('-storesize', $_[1])} |
| 516 |
sub UNSHIFT {$_[0]->callback('-unshift', @_[1 .. $#_])} |
| 517 |
|
| 518 |
############################################################################### |
| 519 |
|
| 520 |
package # temporarily disabled from PAUSE indexer because of permission problems |
| 521 |
Tie::Watch::Hash; |
| 522 |
|
| 523 |
use Carp; |
| 524 |
@Tie::Watch::Hash::ISA = qw/Tie::Watch/; |
| 525 |
|
| 526 |
sub TIEHASH { |
| 527 |
|
| 528 |
my($class, %args) = @_; |
| 529 |
my($variable, $shadow) = @args{-variable, -shadow}; |
| 530 |
my %copy; %copy = %$variable if $shadow; # make a private copy of user's hash |
| 531 |
$args{-ptr} = $shadow ? \%copy : {}; |
| 532 |
my $watch_obj = Tie::Watch->base_watch(%args); |
| 533 |
print "WatchHash new: $variable created, \@_=", join(',', @_), "!\n" |
| 534 |
if $watch_obj->{-debug}; |
| 535 |
bless $watch_obj, $class; |
| 536 |
|
| 537 |
} # end TIEHASH |
| 538 |
|
| 539 |
sub Info {$_[0]->SUPER::Info('-ptr', @Tie::Watch::hash_callbacks)} |
| 540 |
|
| 541 |
# Default hash callbacks. |
| 542 |
|
| 543 |
sub Clear {$_[0]->{-ptr} = ()} |
| 544 |
sub Delete {delete $_[0]->{-ptr}->{$_[1]}} |
| 545 |
sub Destroy {undef %{$_[0]}} |
| 546 |
sub Exists {exists $_[0]->{-ptr}->{$_[1]}} |
| 547 |
sub Fetch {$_[0]->{-ptr}->{$_[1]}} |
| 548 |
sub Firstkey {my $c = keys %{$_[0]->{-ptr}}; each %{$_[0]->{-ptr}}} |
| 549 |
sub Nextkey {each %{$_[0]->{-ptr}}} |
| 550 |
sub Store {$_[0]->{-ptr}->{$_[1]} = $_[2]} |
| 551 |
|
| 552 |
# Hash access methods. |
| 553 |
|
| 554 |
sub CLEAR {$_[0]->callback('-clear')} |
| 555 |
sub DELETE {$_[0]->callback('-delete', $_[1])} |
| 556 |
sub DESTROY {$_[0]->callback('-destroy')} |
| 557 |
sub EXISTS {$_[0]->callback('-exists', $_[1])} |
| 558 |
sub FETCH {$_[0]->callback('-fetch', $_[1])} |
| 559 |
sub FIRSTKEY {$_[0]->callback('-firstkey')} |
| 560 |
sub NEXTKEY {$_[0]->callback('-nextkey')} |
| 561 |
sub STORE {$_[0]->callback('-store', $_[1], $_[2])} |
| 562 |
|
| 563 |
1; |