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:

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:

diff -Naur old/lib/Tie/Scalar.pm new/lib/Tie/Scalar.pm --- old/lib/Tie/Scalar.pm 2018-09-09 12:46:47.358881592 +0200 +++ new/lib/Tie/Scalar.pm 2018-09-09 13:01:56.916476269 +0200 @@ -1,51 +1,40 @@ package Tie::Scalar; -our $VERSION = '1.04'; +our $VERSION = '1.05'; =head1 NAME -Tie::Scalar, Tie::StdScalar - base class definitions for tied scalars +Tie::Scalar - base class definition for tied scalars =head1 SYNOPSIS package NewScalar; - require Tie::Scalar; - @ISA = qw(Tie::Scalar); + use strict; + use warnings; + use parent 'Tie::Scalar'; sub FETCH { ... } # Provide a needed method sub TIESCALAR { ... } # Overrides inherited method - - package NewStdScalar; - require Tie::Scalar; - - @ISA = qw(Tie::StdScalar); - - # All methods provided by default, so define - # only what needs be overridden - sub FETCH { ... } - - package main; + use strict; + use warnings; + tie $new_scalar, 'NewScalar'; - tie $new_std_scalar, 'NewStdScalar'; =head1 DESCRIPTION -This module provides some skeletal methods for scalar-tying classes. +See +This class provides some skeletal methods for scalar-tying classes. S +ee L<perltie> for a list of the functions required in tying a scalar to +a -package. The basic B<Tie::Scalar> package provides a C<new> method, a +s well -as methods C<TIESCALAR>, C<FETCH> and C<STORE>. The B<Tie::StdScalar> -package provides all the methods specified in L<perltie>. It inherit +s from -B<Tie::Scalar> and causes scalars tied to it to behave exactly like t +he -built-in scalars, allowing for selective overloading of methods. The +C<new> +package. The class provides a C<new> method, as well +as methods C<TIESCALAR>, C<FETCH> and C<STORE>. The C<new> method is provided as a means of grandfathering, for classes that for +get to provide their own C<TIESCALAR> method. For developers wishing to write their own tied-scalar classes, the me +thods -are summarized below. The L<perltie> section not only documents these +, but +are summarized below. L<perltie> not only documents these, but has sample code as well: =over 4 @@ -93,6 +82,8 @@ =cut +use strict; +use warnings; use Carp; use warnings::register; @@ -135,30 +126,5 @@ croak "$pkg doesn't define a STORE method"; } -# -# The Tie::StdScalar package provides scalars that behave exactly lik +e -# Perl's built-in scalars. Good base to inherit from, if you're only +going to -# tweak a small bit. -# -package Tie::StdScalar; -@ISA = qw(Tie::Scalar); - -sub TIESCALAR { - my $class = shift; - my $instance = @_ ? shift : undef; - return bless \$instance => $class; -} - -sub FETCH { - return ${$_[0]}; -} - -sub STORE { - ${$_[0]} = $_[1]; -} - -sub DESTROY { - undef ${$_[0]}; -} 1; diff -Naur old/lib/Tie/StdScalar.pm new/lib/Tie/StdScalar.pm --- old/lib/Tie/StdScalar.pm 1970-01-01 01:00:00.000000000 +0100 +++ new/lib/Tie/StdScalar.pm 2018-09-09 13:01:18.284477721 +0200 @@ -0,0 +1,75 @@ +package Tie::StdScalar; + +our $VERSION = '1.05'; + +=head1 NAME + +Tie::StdScalar - base class definition for tied scalars + +=head1 SYNOPSIS + + package NewStdScalar; + + use strict; + use warnings + use parent 'Tie::StdScalar'; + + # All methods provided by default, so define + # only what needs be overridden + sub FETCH { ... } + + package main; + + use strict; + use warnings + use NewStdScalar; + + tie $new_std_scalar, 'NewStdScalar'; + +=head1 DESCRIPTION + +This class provides all the methods specified in L<perltie>. +It causes scalars tied to it to behave exactly like the +built-in scalars, allowing for selective overloading of methods. + +For developers wishing to write their own tied-scalar classes, the me +thods +are summarized in L<Tie::Scalar>. L<perltie> not only documents these +, but +has sample code as well. + +=head1 MORE INFORMATION + +The L<perltie> section uses a good example of tying scalars by associ +ating +process IDs with priority. + +See also L<Tie::Scalar> + +=cut + +use strict; +use warnings; + +# +# The Tie::StdScalar package provides scalars that behave exactly lik +e +# Perl's built-in scalars. Good base to inherit from, if you're only +going to +# tweak a small bit. +# + +sub TIESCALAR { + my $class = shift; + my $instance = @_ ? shift : undef; + return bless \$instance => $class; +} + +sub FETCH { + return ${$_[0]}; +} + +sub STORE { + ${$_[0]} = $_[1]; +} + +sub DESTROY { + undef ${$_[0]}; +} + +1;

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". ;-)