Alter
view release on metacpan or search on metacpan
use Alter ego => {};
imports the "ego()" function and specifies a hash tape for
autovivification. With autovivification you will usually not need to
import the "alter" function at all.
Specifying "NOAUTO" in place of a type specification switches
autovivification off for the current class. This is also the default.
Serialization Support
Serialization is supported for human inspection in "Data::Dumper" style
and for disk storage and cloning in "Storable" style.
For "Data::Dumper" support "Alter" provides the class "Alter::Dumper"
for use as a base class, which contains the single method "Dumper".
"Dumper" returns a string that represents a hash in "Data::Dumper"
format. The hash shows all *alter ego*s that have been created for the
object, keyed by class. An additional key "(body)" (which can't be a
class name) holds the actual body of the object. Formatting- and other
options of "Data::Dumper" will change the appearance of the dump string,
with the exception of $Data::Dumper::Purity, which will always be 1.
"Dumper" can also be imported from "Alter" directly.
Note that "eval()"-ing a dump string will *not* restore the object, but
rather create a hash as described. Re-creation of an object is only
available through "Storable".
For "Storable" support the class "Alter::Storable" is provided with the
methods "STORABLE_freeze", "STORABLE_thaw" and "STORABLE_attach". The
three functions are also exported by "Alter" Their interaction with
"Storable" is described there.
Alter objects support the same data model as traditional Perl objects.
To each class, an Alter object presents an arbitrary reference, the
object's *alter ego*. The type of reference and how it is used are the
entirely the class's business. In particular, the common practice of
using a hash whose keys represent object fields still applies, only each
class sees its individual hash.
"Alter" based objects are garbage-collected and thread-safe without
additional measures.
"Alter" also supports "Data::Dumper" and "Storable" in a generic way, so
that "Alter" based objects can be easily be viewed and made persistent
(within the limitations of the respective modules).
"Alter" works by giving every object a class-specific *alter ego*, which
can be any scalar, for its (the classe's) specific needs for data
storage. The alter ego is set by the "alter()" function (or by
autovivification), usually once per class and object at initialization
time. It is retrieved by the "ego()" function in terms of which a class
will define its accessors.
lib/Alter.pm view on Meta::CPAN
my ( $from, $to) = @_;
my $type = reftype $from;
croak "Incompatible types in STORABLE_thaw" unless
$type eq reftype $to;
croak "Unsupported type '$type' in STORABLE_thaw" unless
my $trans = $trans_tab{ $type};
$trans->( $_[ 0], $_[ 1]); # may change $_[ 1] ($to)
$_[ 1];
}
### Data::Dumper support (for viewing only)
{
package Alter::Dumper;
# return a viewable string containing the object information
sub Dumper {
my $obj = shift;
require Data::Dumper;
local $Data::Dumper::Purity = 1;
Data::Dumper::Dumper( $obj->Alter::image);
}
}
### Storable support
{
package Alter::Storable;
my $running; # indicate if the call is (indirectly) from ourselves
sub STORABLE_freeze {
my ( $obj, $cloning) = @_;
lib/Alter.pm view on Meta::CPAN
imports the C<ego()> function and specifies a hash tape for
autovivification. With autovivification you will usually
not need to import the C<alter> function at all.
Specifying C<"NOAUTO"> in place of a type specification switches
autovivification off for the current class. This is also the
default.
=head3 Serialization Support
Serialization is supported for human inspection in C<Data::Dumper>
style and for disk storage and cloning in C<Storable> style.
For C<Data::Dumper> support C<Alter> provides the class C<Alter::Dumper>
for use as a base class, which contains the single method C<Dumper>.
C<Dumper> returns a string that represents a hash in C<Data::Dumper>
format. The hash shows all I<alter ego>s that have been created for
the object, keyed by class. An additional key "(body)" (which can't
be a class name) holds the actual body of the object. Formatting-
and other options of C<Data::Dumper> will change the appearance of
the dump string, with the exception of C<$Data::Dumper::Purity>,
which will always be 1. C<Dumper> can also be imported from
C<Alter> directly.
Note that C<eval()>-ing a dump string will I<not> restore the
object, but rather create a hash as described. Re-creation of
an object is only available through C<Storable>.
For C<Storable> support the class C<Alter::Storable> is provided
with the methods C<STORABLE_freeze>, C<STORABLE_thaw> and
C<STORABLE_attach>. The three functions are also exported by C<Alter>
lib/Alter.pm view on Meta::CPAN
Alter objects support the same data model as traditional Perl
objects. To each class, an Alter object presents an arbitrary
reference, the object's I<alter ego>. The type of reference and
how it is used are the entirely the class's business. In particular,
the common practice of using a hash whose keys represent object
fields still applies, only each class sees its individual hash.
C<Alter> based objects are garbage-collected and thread-safe without
additional measures.
C<Alter> also supports C<Data::Dumper> and C<Storable> in
a generic way, so that C<Alter> based objects can be easily be viewed
and made persistent (within the limitations of the respective modules).
C<Alter> works by giving every object a class-specific I<alter ego>,
which can be any scalar, for its (the classe's) specific needs for
data storage. The alter ego is set by the C<alter()> function (or
by autovivification), usually once per class and object at initialization
time. It is retrieved by the C<ego()> function in terms of which
a class will define its accessors.
t/03_class.t view on Meta::CPAN
# Class_A is a conventional hash-based class with two fields one_A and two_A
# Class_B is an Alter-based class with fields of one_B and two_B stored in an
# array.
# Both classes have an init() method that works as a creator when called
# as a class method. There are read-only accessors to the fields
# Class_C is a hybrid class inheriting both Class_A and Class_B
# For tests we set fields one_A and one_B to plain scalars. two_A
# and two_B are set to hold a reference to the same array. This identity
# must survive a freeze-thaw cycle by either Data::Dumper or Storable
### Class_A
{
package Class_A;
sub init {
my $obj = shift;
$obj = bless {}, $obj unless ref $obj;
$obj->{ one_A} = shift;
t/03_class.t view on Meta::CPAN
is $clone->one_B, $one_B, "Cloned one_B (thaw)";
isnt $clone->two_A, $two_A, "Cloned ref different (thaw)";
is ref $clone->two_A, 'ARRAY', "Cloned ref type (thaw)";
is $clone->two_B, $clone->two_A, "Cloned ref identity (thaw)";
BEGIN { $n_tests += 7 }
}
### Dumper
{
use Data::Dumper;
my ( $one_A, $two_A) = ( 'haha', []);
my ( $one_B, $two_B) = ( 'hihi', $two_A);
my $cc = Class_C->init( $one_A, $two_A, $one_B, $two_B);
my $dump = $cc->Dumper;
# diag $dump;
my $image = do {
my $VAR1;
( run in 0.680 second using v1.01-cache-2.11-cpan-a5abf4f5562 )