in reply to Breaking Tie::Hash into three modules

More mess

Tie::Array and Tie::Scalar also place Tie::StdArray and Tie::StdScalar in the wrong files. Tie::Scalar also copies the useless new() method from Tie::Hash, including the warnings generated by the real constructor (TIESCALAR() / TIEHASH()).

Tie::Array does not. It does not even implement a TIEARRAY() constructor. I think this is the cleanest solution. If inheriting classes do not implement TIEARRAY(), perl will automatically complain, no code required at all.

Tie::Array implements a dummy DESTROY() method. I don't think it is strictly needed, but it should not hurt.

Tie::Handle does not embed Tie::StdHandle. It was moved out into Tie/StdHandle.pm in 2007 and replaced by a backwards-compatible use Tie::StdHandle;. But it also copies the useless new() method.

More patches

I've written two more patches, that split out the Tie::Std* classes, add strict and warnings, and use parent instead of require and assigning to @ISA.

Tie::Array

Note: Tie::StdArray does not inherit from Tie::Array, because it would inherit only the empty methods EXTEND() and DESTROY(). Instead, it implements its own empty EXTEND() and DESTROY() methods.

Based on https://perl5.git.perl.org/perl.git/blob/83aebc99b27428f0566bab5ded4d1df2167a9d4a:/lib/Tie/Array.pm:

diff -Naur old/lib/Tie/Array.pm new/lib/Tie/Array.pm --- old/lib/Tie/Array.pm 2018-09-09 12:21:08.579390275 +0200 +++ new/lib/Tie/Array.pm 2018-09-09 12:37:13.387307122 +0200 @@ -3,7 +3,8 @@ use 5.006_001; use strict; use Carp; -our $VERSION = '1.07'; +use Tie::StdArray; +our $VERSION = '1.08'; # Pod documentation after __END__ below. @@ -82,32 +83,6 @@ croak "$pkg doesn't define a DELETE method"; } -package Tie::StdArray; -our @ISA = 'Tie::Array'; - -sub TIEARRAY { bless [], $_[0] } -sub FETCHSIZE { scalar @{$_[0]} } -sub STORESIZE { $#{$_[0]} = $_[1]-1 } -sub STORE { $_[0]->[$_[1]] = $_[2] } -sub FETCH { $_[0]->[$_[1]] } -sub CLEAR { @{$_[0]} = () } -sub POP { pop(@{$_[0]}) } -sub PUSH { my $o = shift; push(@$o,@_) } -sub SHIFT { shift(@{$_[0]}) } -sub UNSHIFT { my $o = shift; unshift(@$o,@_) } -sub EXISTS { exists $_[0]->[$_[1]] } -sub DELETE { delete $_[0]->[$_[1]] } - -sub SPLICE -{ - my $ob = shift; - my $sz = $ob->FETCHSIZE; - my $off = @_ ? shift : 0; - $off += $sz if $off < 0; - my $len = @_ ? shift : $sz-$off; - return splice(@$ob,$off,$len,@_); -} - 1; __END__ @@ -119,8 +94,10 @@ =head1 SYNOPSIS package Tie::NewArray; - use Tie::Array; - @ISA = ('Tie::Array'); + + use strict; + use warnings; + use parent 'Tie::Array'; # mandatory methods sub TIEARRAY { ... } @@ -142,18 +119,13 @@ sub EXTEND { ... } sub DESTROY { ... } - package Tie::NewStdArray; - use Tie::Array; - - @ISA = ('Tie::StdArray'); - - # all methods provided by default - package main; + use strict; + use warnings; + use Tie::NewArray; + $object = tie @somearray,'Tie::NewArray'; - $object = tie @somearray,'Tie::StdArray'; - $object = tie @somearray,'Tie::NewStdArray'; @@ -168,11 +140,6 @@ C<UNSHIFT>, C<SPLICE> and C<CLEAR> in terms of basic C<FETCH>, C<STOR +E>, C<FETCHSIZE>, C<STORESIZE>. -The B<Tie::StdArray> package provides efficient methods required for +tied arrays -which are implemented as blessed references to an "inner" perl array. -It inherits from B<Tie::Array>, and should cause tied arrays to behav +e exactly -like standard arrays, allowing for selective overloading of methods. - For developers wishing to write their own tied arrays, the required m +ethods are briefly defined below. See the L<perltie> section for more detail +ed descriptive, as well as example code: @@ -270,6 +237,37 @@ =back +=head1 Legacy + +Old versions of Tie::Array up to 1.07 included the class B<Tie::StdAr +ray>, +requiring the following workaround to inherit from B<Tie::StdArray>: + + package NewStdArray; + + use strict; + use warnings; + + use Tie::Hash; + our @ISA = qw( Tie::StdHash ); + +To provide compatibility with old code, B<Tie::Array> automatically l +oads +B<Tie::StdArray>. + +New code should load and inherit from B<Tie::Array> and B<Tie::StdArr +ay> +via C<use parent>: + + package NewArray; + + use strict; + use warnings; + use parent 'Tie::Array'; + + package NewStdArray; + + use strict; + use warnings; + use parent 'Tie::StdArray'; + =head1 CAVEATS There is no support at present for tied @ISA. There is a potential co +nflict diff -Naur old/lib/Tie/StdArray.pm new/lib/Tie/StdArray.pm --- old/lib/Tie/StdArray.pm 1970-01-01 01:00:00.000000000 +0100 +++ new/lib/Tie/StdArray.pm 2018-09-09 12:43:12.403810384 +0200 @@ -0,0 +1,92 @@ +package Tie::StdArray; + +use 5.006_001; +use strict; +use Carp; +our $VERSION = '1.08'; + +# Pod documentation after __END__ below. + +sub DESTROY { } +sub EXTEND { } + +sub TIEARRAY { bless [], $_[0] } +sub FETCHSIZE { scalar @{$_[0]} } +sub STORESIZE { $#{$_[0]} = $_[1]-1 } +sub STORE { $_[0]->[$_[1]] = $_[2] } +sub FETCH { $_[0]->[$_[1]] } +sub CLEAR { @{$_[0]} = () } +sub POP { pop(@{$_[0]}) } +sub PUSH { my $o = shift; push(@$o,@_) } +sub SHIFT { shift(@{$_[0]}) } +sub UNSHIFT { my $o = shift; unshift(@$o,@_) } +sub EXISTS { exists $_[0]->[$_[1]] } +sub DELETE { delete $_[0]->[$_[1]] } + +sub SPLICE +{ + my $ob = shift; + my $sz = $ob->FETCHSIZE; + my $off = @_ ? shift : 0; + $off += $sz if $off < 0; + my $len = @_ ? shift : $sz-$off; + return splice(@$ob,$off,$len,@_); +} + +1; + +__END__ + +=head1 NAME + +Tie::StdArray - base class for tied arrays + +=head1 SYNOPSIS + + package Tie::NewStdArray; + + use strict; + use warnings; + use parent 'Tie::StdArray'; + + # all methods provided by default + + package main; + + use strict; + use warnings; + use Tie::NewStdArray; + + $object = tie @somearray,'Tie::NewStdArray'; + +=head1 DESCRIPTION + +This module provides methods for array-tying classes. See +L<perltie> for a list of the functions required in order to tie an ar +ray +to a package. + +This class provides stub C<DESTROY>, and C<EXTEND> methods that do no +thing, +and provides efficient methods required for tied arrays +which are implemented as blessed references to an "inner" perl array. +It should cause tied arrays to behave exactly +like standard arrays, allowing for selective overloading of methods. + +For developers wishing to write their own tied arrays, the required m +ethods +are briefly defined in L<Tie::Array>. See the L<perltie> section for +more +detailed descriptive, as well as example code. + +=head1 MORE INFORMATION + +See L<Tie::Array> + +=head1 CAVEATS + +There is no support at present for tied @ISA. There is a potential co +nflict +between magic entries needed to notice setting of @ISA, and those nee +ded to +implement 'tie'. + +=head1 AUTHOR + +Nick Ing-Simmons E<lt>nik@tiuk.ti.comE<gt> + +=cut

Tie::Scalar

Note: The new Tie::StdScalar does not have a new() method at all. It is not needed, for the same reasons as explained in Re: Breaking Tie::Hash into three modules for Tie::StdHash. It also does not inherit from Tie::Scalar, because it would reimplement all methods (except for new()).

Based on https://perl5.git.perl.org/perl.git/blob/2515a12cf493baaa211da7524a276dd30695ca29:/lib/Tie/Scalar.pm:

Tie::Handle

No patch. I can use parent 'Tie::StdHandle';, so I don't really need to patch here. The synopsis in Tie::StdHandle is wrong (copied from Tie::Handle), but it is quite obvious that only the class name has to be changed to Tie::StdHandle.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)