A wormhole between call frames
package A; sub new { bless {}, shift } sub a { B->new->b } package B; sub new { bless {}, shift } sub b { C->new->c } package C; sub new { bless {}, shift } sub c { ref pop } package main; print ref A->new->a; # without aspect, prints C use Aspect::Library::Wormhole; aspect Wormhole => 'A::a', 'C::c'; print ref A->new->a; # with aspect, prints A
A reusable aspect for passing objects down a call flow, without adding extra arguments to the frames between the source and the target. It is a tool for acquiring implicit context.
Suppose \*(C`A::a()\*(C' calls \*(C`B::b()\*(C' calls \*(C`C::c()\*(C'... until \*(C`Z::z()\*(C'.
All is well, until one day you get a requirement with a crosscutting implication- \*(C`Z::Z()\*(C' requires one extra argument. It requires an instance of the class \*(C`A\*(C'. The very same instance on which the method \*(C`a()\*(C' was called, high up the call chain of \*(C`Z::z()\*(C'.
Without this aspect you can either add a global $Current_A (very problematic), or make \*(C`A::a()\*(C' send \*(C`B::b()\*(C' its $self, make \*(C`B::b()\*(C' pass it on to \*(C`C::c()\*(C', and so on until \*(C`Z::z()\*(C'. You are forced to add many arguments to many methods.
Show me a developer who has never encountered this situation: you need to add an argument to a long call flow, just because someone at the bottom needs it, yet only someone on the top has it. The monkey code required on each call frame in the call flow, for each argument that each target requires, is suffering from \s-1EEK\s0- Extraneous Embedded Knowledge (<http://citeseer.ist.psu.edu/254612.html>).
The code for the frames between the two ends of the wormhole, knows more about the world than it should. This extraneous knowledge is embedded in each method on the call flow, and there is no easy way to remove it.
This aspect removes the \s-1EEK\s0 by allowing you to setup a wormhole between the source and target frames in the call flow. The only effect the wormhole has on the call flow, is that the target gets called with one extra argument: the calling source object. Thus the target acquires implicit context.
So this wormhole:
aspect Wormhole => 'A::a', 'Z::z';
Means: before the method \*(C`Z::z()\*(C' is called, if \*(C`A::a()\*(C' exists in the call flow, then append one argument to the argument list of \*(C`Z::z()\*(C'. The argument appended is the calling \*(C`A\*(C' object.
No method in the call flow is required to pass the source object, but \*(C`Z::z()\*(C' will still receive it.
+--------+ +--------+ | source | +--------+ +--------+ | target | +--------+--> | B::b() |--> | C::c() |--> ...--> +--------+ | A::a() | +--------+ +--------+ | Z::z() | +--------+ +--------+ . , | /|\ | / | \ | | +------------- The Bajoran Wormhole -------------+
The aspect constructor takes two pointcut specs, a source and a target. The spec can be a string (full sub name), a regex (sub will match if rexep matches), or a coderef (called with sub name, will match if returns true).
For example, this will append a calling \*(C`Printer\*(C' to any call to a sub defined on \*(C`Page\*(C', if it is in the call flow of \*(C`Printer::print\*(C':
aspect Wormhole => 'Printer::Print', qr/^Page::/;
Copyright 2001 by Marcel Gru\*:nauer
Some parts copyright 2009 - 2013 Adam Kennedy.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.