Headline

Implementation in Language:Perl using Technology:Moops as a saner object system.

Characteristics

Perl is a general-purpose scripting language, usually grouped into the same family as Language:Python and having inspired Language:Ruby. This contribution also uses Moops as a OO_programming extension.

Moops actually extends Perl's syntax itself - something relatively common in Perl, but rarely seen in other languages.

The best feature of Perl however cannot be really shown directly in this contribution: the central module repository called Perl Archive Network (CPAN), which is very complete, tested and makes installing even compiled modules trivial. This contribution uses various non-core modules from there.

Illustration

Data Model

The concrete classes of the object model are based on the Model class that provides names, unique IDs (UUIDs), a list of children and supporting methods. Moops adds Syntactic sugar, Type checking and reduces Boilerplate code by automatically providing Constructors and accessors (Getters and Setters, but rolled into one method) for the object's properties.

class Model {
    has 'text' => (
        is       => 'rw',
        isa      => 'Str',
        required => 1,
    );

    has 'id' => (
        is       => 'rw',
        isa      => 'Str',
        required => 1,
        default  => \&create_uuid,
    );

    has 'children' => (
        is       => 'rw',
        isa      => 'ArrayRef[C101::Model]',
        required => 1,
        default  => sub { [] },
    );

    # methods omitted
}

The concrete objects now just extend the model and add additional fields, if any:

# methods omitted

class Company    extends Model { ... }

class Department extends Model { ... }

class Employee   extends Model {
    has 'address' => (
        is       => 'rw',
        isa      => 'Str',
        required => 1,
    );

    has 'salary' => (
        is       => 'rw',
        isa      => 'UnsignedNum',
        required => 1,
    );
}

Operations

Features like total, cut or depth are all implemented via a variation of the Visitor_pattern. Since Perl supports closures, even performing folding operations is very simple. For example, the implementation of total:

sub total {
    my $total   = 0;
    my $visitor = C101::Visitor->new({
        begin_employee => sub {
            $total += $_[1]->salary; # note: $_[1] is the employee, see documentation
        },
    });
    $_->visit($visitor) for @_; # note: @_ is the list of arguments
    return $total;
}

To support more complicated restructuring, the visitor passes the list it is iterating through and the loop index into the callbacks. This allows for example to perform deletion of an element and appropriately decrementing the list index:

# callback for remove operation
my $callback = sub {
    my ($visitor, $thing, $list, $index) = @_;
    splice($list, $$index--, 1) if $should_remove->($thing);
};

Persistence

Serialization, deserialization, parsing and unparsing are all just simple one-liners that dispatch to the appropriate modules:

# note: @_ is the list of arguments
# Binary storage with Storable module
sub serialize   {      store(@_) }
sub unserialize {   retrieve(@_) }
# Human-readable storage with YAML module
sub parse       { YAML::Load(@_) }
sub unparse     { YAML::Dump(@_) }

Language:YAML was chosen because Perl objects can be directly represented in the format (as opposed to for example Language:JSON). For example, an Employee object would be encoded as such:

!!perl/hash:C101::Employee
address  : Redmond
children : []
name     : Ray
salary   : 234567
uuid     : F27EE512-1005-11E4-ABC1-30B06F9AEFE7

Relationships

Contribution:dancer extends this contribution with a Feature:Web_UI.

Architecture

The logical parts of the contribution are separated into Perl Module (.pm) files in a Namespace called C101. The necessary modules are loaded in the files where they are required.

The actual executable file for testing is a single Perl script. It uses the Test::More module, which is Perl's core testing framework.

Usage

Please see the readme.pod on GitHub for details.


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.