Alter module on CPAN

A

anno4000

I have put the module Alter on CPAN.

Alter introduces "Alter Ego" objects as an alternative to the inside-out
technique of object construction, with similar freedom of inheritance.
It is an XS module, though a pure Perl fallback is provided.

Alter-based objects are easier to deal with than inside-out objects,
mostly because the object data model is unchanged. In particular,
an object can be constructed in the common way as a hash whose entries
are the object data keyed by field name.

In addition, Alter objects are garbage-collected and thread-safe by
construction. Inside-out objects need extra support for that.

Thirdly, Alter objects are dumpable (Data::Dumper style) and
storable (Storable style). For this, additional support is necessary
even for Alter objects. That support is provided.

Alter classes work by invoking the function Alter::ego() (importable)
on objects for every data access, much like inside-out classes call
an id() function on the object for data access. ego( $obj) retrieves
a *class specific* reference from the object which is independent from
the reference the object actually is. Thus every class gets to decide
what data type to use and how to store its data in it, without interfering
with a neighbor class that has different ideas. A traditional class that
stores the data in the object body can continue to do so.

Below is an example that demonstrates the ability of Alter classes
to combine with a traditional class. A trivial Alter-based class
"Name" is created whose objects hold a single string accessed as
->name. A second class, "NamedFh" is based on that and the
standard class IO::File. Methods of both IO::File and Name can
successfully be invoked on objects of this combined class.

[Update: Error in code corrected]

Anno

my $n = Name->init( 'Otto');
print $n->name, "\n";

my $fh = NamedFh->new( '/dev/null', 'BitBucket');
$fh->eof() and print "'IO::Handle' method works\n";
print "'Name' method retrieves '", $fh->name, "'\n";
exit;

#######################################################################

package Name;
use Alter ego => {};

sub init {
my $obj = shift;
# create if called as class method
$obj = bless do { \ my %o }, $obj unless ref $obj;
ego( $obj)->{ name} = shift;
$obj;
}

sub name { ego( shift)->{ name} }

package NamedFh;
use base 'Name';
use base 'IO::File';

sub new {
my $class = shift;
my ( $file, $name) = @_;
my $nh = $class->IO::File::new( $file);
$nh->Name::init( $name);
}
__END__
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top