X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperltie.pod;h=4befdaeee671d7b2bd65d2c30fa1549ec8105161;hb=363c40c40eaf5d0cfd92f460a3f838c41f9756ad;hp=8b3f6799a9f8dfe5496382f933a73695992b161b;hpb=4ae8561836c8753561ec7b81c4d794440b8657c3;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perltie.pod b/pod/perltie.pod index 8b3f679..4befdae 100644 --- a/pod/perltie.pod +++ b/pod/perltie.pod @@ -161,7 +161,7 @@ argument--the new value the user is trying to assign. This method will be triggered when the C occurs. This can be useful if the class needs to know when no further calls will be made. (Except DESTROY -of course.) See below for more details. +of course.) See L Gotcha> below for more details. =item DESTROY this @@ -258,7 +258,9 @@ index whose value we're trying to fetch. If a negative array index is used to read from an array, the index will be translated to a positive one internally by calling FETCHSIZE -before being passed to FETCH. +before being passed to FETCH. You may disable this feature by +assigning a true value to the variable C<$NEGATIVE_INDICES> in the +tied array class. As you may have noticed, the name of the FETCH method (et al.) is the same for all accesses, even though the constructors differ in names (TIESCALAR @@ -311,15 +313,19 @@ deleted. In our example, 'undef' is really an element containing C<$self-E{ELEMSIZE}> number of spaces. Observe: -sub STORESIZE { - my $self = shift; - my $count = shift; - if ( $count > $self->FETCHSIZE() ) { - $self->STORE( $_, '' ) foreach $count - $self->FETCHSIZE() + 1 .. $count; - } elsif ( $count < $self->FETCHSIZE() ) { - $self->POP() foreach 0 .. $self->FETCHSIZE() - $count + 1; - } -} + sub STORESIZE { + my $self = shift; + my $count = shift; + if ( $count > $self->FETCHSIZE() ) { + foreach ( $count - $self->FETCHSIZE() .. $count ) { + $self->STORE( $_, '' ); + } + } elsif ( $count < $self->FETCHSIZE() ) { + foreach ( 0 .. $self->FETCHSIZE() - $count - 2 ) { + $self->POP(); + } + } + } =item EXTEND this, count @@ -346,14 +352,16 @@ C<$self-E{ELEMSIZE}> spaces only, it does not exist: sub EXISTS { my $self = shift; my $index = shift; - return $self->{ARRAY}->[$index] eq ' ' x $self->{ELEMSIZE} ? 0 : 1; + return 0 if ! defined $self->{ARRAY}->[$index] || + $self->{ARRAY}->[$index] eq ' ' x $self->{ELEMSIZE}; + return 1; } =item DELETE this, key Delete the element at index I from the tied array I. -In our example, a deleted item is C<$self->{ELEMSIZE}> spaces: +In our example, a deleted item is C<$self-E{ELEMSIZE}> spaces: sub DELETE { my $self = shift; @@ -446,7 +454,7 @@ In our example, we'll use a little shortcut if there is a I: =item UNTIE this -Will be called when C happens. (See below.) +Will be called when C happens. (See L Gotcha> below.) =item DESTROY this @@ -457,17 +465,6 @@ just leave it out. =back -The code we presented at the top of the tied array class accesses many -elements of the array, far more than we've set the bounds to. Therefore, -it will blow up once they try to access beyond the 2nd element of @ary, as -the following output demonstrates: - - setting index 0: value of elt 0 now 0 - setting index 1: value of elt 1 now 10 - setting index 2: value of elt 2 now 20 - setting index 3: Array OOB: 3 > 2 at Bounded_Array.pm line 39 - Bounded_Array::FETCH called at testba line 12 - =head2 Tying Hashes Hashes were the first Perl data type to be tied (see dbmopen()). A class @@ -480,7 +477,7 @@ the keys. UNTIE is called when C happens, and DESTROY is called when the tied variable is garbage collected. If this seems like a lot, then feel free to inherit from merely the -standard Tie::Hash module for most of your methods, redefining only the +standard Tie::StdHash module for most of your methods, redefining only the interesting ones. See L for details. Remember that Perl distinguishes between a key not existing in the hash, @@ -761,7 +758,7 @@ thing, but we'll have to go through the LIST field indirectly. =item UNTIE this -This is called when C occurs. +This is called when C occurs. See L Gotcha> below. =item DESTROY this @@ -797,9 +794,16 @@ READ, and possibly CLOSE, UNTIE and DESTROY. The class can also provide: BINMOD OPEN, EOF, FILENO, SEEK, TELL - if the corresponding perl operators are used on the handle. -It is especially useful when perl is embedded in some other program, -where output to STDOUT and STDERR may have to be redirected in some -special way. See nvi and the Apache module for examples. +When STDERR is tied, its PRINT method will be called to issue warnings +and error messages. This feature is temporarily disabled during the call, +which means you can use C inside PRINT without starting a recursive +loop. And just like C<__WARN__> and C<__DIE__> handlers, STDERR's PRINT +method may be called to report parser errors, so the caveats mentioned under +L apply. + +All of this is especially useful when perl is embedded in some other +program, where output to STDOUT and STDERR may have to be redirected +in some special way. See nvi and the Apache module for examples. In our example we're going to create a shouting handle. @@ -855,7 +859,7 @@ or C functions. sub READ { my $self = shift; - my $$bufref = \$_[0]; + my $bufref = \$_[0]; my(undef,$len,$offset) = @_; print "READ called, \$buf=$bufref, \$len=$len, \$offset=$offset"; # add to $$bufref, set $len to number of characters read @@ -885,7 +889,8 @@ function. =item UNTIE this As with the other types of ties, this method will be called when C happens. -It may be appropriate to "auto CLOSE" when this occurs. +It may be appropriate to "auto CLOSE" when this occurs. See +L Gotcha> below. =item DESTROY this @@ -908,7 +913,7 @@ Here's how to use our little example: =head2 UNTIE this You can define for all tie types an UNTIE method that will be called -at untie(). +at untie(). See L Gotcha> below. =head2 The C Gotcha @@ -1067,6 +1072,21 @@ modules L, L, L, or L. =head1 BUGS +The bucket usage information provided by C is not +available. What this means is that using %tied_hash in boolean +context doesn't work right (currently this always tests false, +regardless of whether the hash is empty or hash elements). + +Localizing tied arrays or hashes does not work. After exiting the +scope the arrays or the hashes are not restored. + +Counting the number of entries in a hash via C +or C) is inefficient since it needs to iterate +through all the entries with FIRSTKEY/NEXTKEY. + +Tied hash/array slices cause multiple FETCH/STORE pairs, there are no +tie methods for slice operations. + You cannot easily tie a multilevel data structure (such as a hash of hashes) to a dbm file. The first problem is that all but GDBM and Berkeley DB have size limitations, but beyond that, you also have problems @@ -1086,4 +1106,4 @@ TIEHANDLE by Sven Verdoolaege > and Doug MacEachern > -Tying Arrays by Casey Tweten > +Tying Arrays by Casey West >