aspect-oriented programming in perl

Download Aspect-oriented programming in Perl

If you can't read please download the document

Upload: megakott

Post on 01-Jul-2015

1.602 views

Category:

Technology


6 download

DESCRIPTION

1.) Paradigm of "Aspect-oriented programming"2.) Its implementation on Perl with Aspect.pm

TRANSCRIPT

Aspect-oriented programming
in Perl

Speaker: Aleksandr [email protected]

Contents

Aspect-Oriented Programming

In Perl

AOP is a kind of trending topic now

Howewer, not so popular as Perl...

Back to contents

Aspect-Oriented Programming

In Perl

The Goal: just overview of AOP :-)

So, paradigm

Aspect-oriented programming
is a modern trending paradigm

Procedure programming

Object-oriented programming

Aspect-oriented programming

Wikipedia

In computing, aspect-oriented programming (AOP) is a programming paradigm which isolates secondary or supporting functions from the main program's business logic.

AOP is a step to AOSD

It aims to increase modularity by allowing the separation of cross-cutting concerns, forming a basis for aspect-oriented software development.

Aspect-oriented software development

In computing, Aspect-oriented software development (AOSD) is an emerging software development technology that seeks new modularizations of software systems in order to isolate secondary or supporting functions from the main program's business logic. AOSD allows multiple concerns to be expressed separately and automatically unified into working systems.

History

AOP as such has a number of antecedents: the Visitor Design Pattern, CLOS MOP, and others. Gregor Kiczales and colleagues at Xerox PARC developed AspectJ (perhaps the most popular general-purpose AOP package) and made it available in 2001. IBM's research team emphasized the continuity of the practice of modularizing concerns with past programming practice, and in 2001 offered the more powerful (but less usable) Hyper/J and Concern Manipulation Environment, which have not seen wide usage. EmacsLisp changelog added AOP related code in version 19.28. The examples in this article use AspectJ as it is the most widely known of AOP languages

Motivation

...That means to change logging can require modifying all affected modules...

Examples of aspects

One example of such aspects are design patterns, which combine various kinds of classes to produce a common type of behavior. Another is logging.

Contents

Aspect-Oriented Programming

In Perl

Aspect.pm (1 / 2)

http://search.cpan.org/~adamk/Aspect-0.92/lib/Aspect.pm

By Adam Kennedy (Australia)

http://search.cpan.org/~adamk/

Top 1 CPAN author: > 230 modules totally

http://en.wikipedia.org/wiki/Adam_Kennedy_(programmer

Use 5.008002

History:

V0.01: 28-Sep-2001

V0.95: 13-Dec-2010

Aspect.pm (2 / 2)

The Perl Aspect module closely follows the terminology of the AspectJ project (http://eclipse.org/aspectj)

The Perl Aspect module is focused on subroutine matching and wrapping. It allows you to select collections of subroutines using a flexible pointcut language, and modify their behavior in any way you want.

Terminology

Join Point

Pointcut

Advice

Advice Code

Weave

The Aspect

Join Point

An event that occurs during the running of a program. Currently only calls to subroutines are recognized as join points.

Pointcut

An expression that selects a collection of join points.

For example: all calls to the class Person, that are in the call flow of some Company, but not in the call flow of Company::make_report.

Aspect supports call(), and cflow() pointcuts, and logical operators (&, |, !) for constructing more complex pointcuts. See the Aspect::Pointcut documentation.

Advice

A pointcut, with code that will run when it matches. The code can be run before or after the matched sub is run.

Advice Code

The code that is run before or after a pointcut is matched. It can modify the way that the matched sub is run, and the value it returns.

Weave

The installation of advice code on subs that match a pointcut.

Weaving happens when you create the advice.

Unweaving happens when the advice goes out of scope.

The Aspect

An object that installs advice.

A way to package advice and other Perl code, so that it is reusable.

Features (1 / 3)

Create and remove pointcuts, advice, and aspects.

Flexible pointcut language: select subs to match using string equality, regexp, or CODE ref. Match currently running sub, or a sub in the call flow. Build pointcuts composed of a logical expression of other pointcuts, using conjunction, disjunction, and negation.

Features (2 / 3)

In advice code, you can: modify parameter list for matched sub, modify return value, decide if to proceed to matched sub, access CODE ref for matched sub, and access the context of any call flow pointcuts that were matched, if they exist.

Add/remove advice and entire aspects during run-time. Scope of advice and aspect objects, is the scope of their effect.

Features (3 / 3)

A reusable aspect library. The Wormhole, aspect, for example. A base class makes it easy to create your own reusable aspects. The Memoize aspect is an example of how to interface with AOP-like modules from CPAN.

Alternative - Hook::Lexwrap

Hook::Lexwrap is a backend for Aspect

The Aspect module is different from it in two respects:

Select join points using flexible pointcut language instead of the sub name. For example: select all calls to Account objects that are in the call flow of Company::make_report.

More options when writing the advice code. You can, for example, run the original sub, or append parameters to it.

Motivation, again (1 / 2)

Perl is a highly dynamic language, where everything this module does can be done without too much difficulty. All this module does, is make it even easier, and bring these features under one consistent interface.

Motivation, again (2 / 2)

Adam Kennedy have found it useful in his work in several places:

Saves from typing an entire line of code for almost every Test::Class test method, because using the TestClass aspect.

Using custom advice to modify class behavior: register objects when constructors are called, save object state on changes to it, etc. All this, while cleanly separating these concerns from the effected class. They exist as an independent aspect, so the class remains unpolluted.

Using Aspect.pm

This package is a facade on top of the Perl AOP framework. It allows you to create pointcuts, advice, and aspects in a simple declarative fastion.

You will be mostly working with this package (Aspect), and the advice context package.

When you use Aspect; you will import a family of around a dozen functions. These are all factories that allow you to create pointcuts, advice, and aspects.

Code :-) Pointcuts

String: $p = call 'Person::get_address';

Regexp: $p = call qr/^Person::\w+$/;

CODE ref:
$p = call sub { exists $subs_to_match{shift()} }

$p = call qr/^Person::\w+$/ & ! call 'Person::create';

$p = call qr/^Person::\w+$/ & cflow company => qr/^Company::\w+$/;

Advice

after {

print "Person::get_address has returned!\n";

} call 'Person::get_address';

Aspect

Aspects are just plain old Perl objects, that install advice, and do other AOP-like things, like install methods on other classes, or mess around with the inheritance hierarchy of other classes. A good base class for them is Aspect::Modular, but you can use any Perl object as long as the class inherits from Aspect::Library.

If the aspect class exists immediately below the namespace Aspect::Library, then it can be easily created with the following shortcut.

aspect Singleton => 'Company::create';

The whole example (1 / 4)

package Person;

sub create {

# ...

}

sub set_name {

# ...

}

sub get_address {

# ...

}

The whole example (2 / 4)

package main;

use Aspect;

### USING REUSABLE ASPECTS

# There can only be one.

aspect Singleton => 'Person::create';

# Profile all setters to find any slow ones

aspect Profiler => call qr/^Person::set_/;

The whole example (3 / 4)

### WRITING YOUR OWN ADVICE

# Defines a collection of events

my $pointcut = call qr/^Person::[gs]et_/;

# Advice will live as long as $before is in scope

my $before = before {

print "g/set will soon be next";

} $pointcut;

# Advice will live forever, because it is created in void context

after {

print "g/set has just been called";

} $pointcut;

The whole example (4 / 4)

# Advice runs conditionally based on multiple factors

before {

print "get will be called next, and we are within Tester::run_tests";

} call qr/^Person::get_/ & cflow tester => 'Tester::run_tests';

# Complex condition hijack of a method if some condition is true

around {

if ( $_->self->customer_name eq 'Adam Kennedy' ) {

# Ensure I always have cash

$_->return_value('One meeeelion dollars');

} else {

# Take a dollar off everyone else

$_->proceed;

$_->return_value( $_->return_value - 1 );

}

} call 'Bank::Account::balance';

Usage (in blogs)

http://use.perl.org/ - blogs about Perl

http://use.perl.org/~unimatrix/journal/857 http://use.perl.org/~Alias/journal/40368

Thanks! Questions?!

This presentation will be uploaded into slideshare within a week.