SlideShare a Scribd company logo
Aspect-oriented programming in Perl Speaker: Aleksandr Kotov [email_address]
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.

More Related Content

What's hot (20)

PDF
Function in C
Dr. Abhineet Anand
 
PPT
Cs30 New
DSK Chakravarthy
 
PPTX
INLINE FUNCTION IN C++
Vraj Patel
 
PPTX
Inline function
Tech_MX
 
PPTX
Dost.jar and fo.jar
Suite Solutions
 
PPT
PDF Localization
Suite Solutions
 
PPTX
Inline Functions and Default arguments
Nikhil Pandit
 
PPT
Functions in C++
Nikhil Pandit
 
PPTX
functions in C and types
mubashir farooq
 
PDF
Chapter 11 Function
Deepak Singh
 
PDF
Data types in c++
RushikeshGaikwad28
 
PPTX
Function Parameters
primeteacher32
 
PPTX
Inline functions
DhwaniHingorani
 
PDF
Booting into functional programming
Dhaval Dalal
 
PPT
16717 functions in C++
LPU
 
PPTX
POLITEKNIK MALAYSIA
Aiman Hud
 
PDF
08 -functions
Hector Garzo
 
PPTX
Functions in c language
tanmaymodi4
 
PPTX
Function C++
Shahzad Afridi
 
PPTX
Functions in c++
Rokonuzzaman Rony
 
Function in C
Dr. Abhineet Anand
 
INLINE FUNCTION IN C++
Vraj Patel
 
Inline function
Tech_MX
 
Dost.jar and fo.jar
Suite Solutions
 
PDF Localization
Suite Solutions
 
Inline Functions and Default arguments
Nikhil Pandit
 
Functions in C++
Nikhil Pandit
 
functions in C and types
mubashir farooq
 
Chapter 11 Function
Deepak Singh
 
Data types in c++
RushikeshGaikwad28
 
Function Parameters
primeteacher32
 
Inline functions
DhwaniHingorani
 
Booting into functional programming
Dhaval Dalal
 
16717 functions in C++
LPU
 
POLITEKNIK MALAYSIA
Aiman Hud
 
08 -functions
Hector Garzo
 
Functions in c language
tanmaymodi4
 
Function C++
Shahzad Afridi
 
Functions in c++
Rokonuzzaman Rony
 

Viewers also liked (6)

PPT
Oo Perl
olegmmiller
 
PDF
Perl in the Real World
OpusVL
 
PPTX
Perl
Raghu nath
 
PDF
perl_objects
tutorialsruby
 
PDF
Perl object ?
ℕicolas ℝ.
 
PPTX
Creating Perl modules with Dist::Zilla
Mark Gardner
 
Oo Perl
olegmmiller
 
Perl in the Real World
OpusVL
 
perl_objects
tutorialsruby
 
Perl object ?
ℕicolas ℝ.
 
Creating Perl modules with Dist::Zilla
Mark Gardner
 
Ad

Similar to Aspect-oriented programming in Perl (20)

PPT
Smoothing Your Java with DSLs
intelliyole
 
PPT
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Antonio Peric-Mazar
 
PDF
(1) c sharp introduction_basics_dot_net
Nico Ludwig
 
ODP
Coder Presentation Boston
Doug Green
 
PDF
Working Effectively With Legacy Perl Code
erikmsp
 
ODP
Drupal Best Practices
manugoel2003
 
PPTX
The GO Language : From Beginners to Gophers
I.I.S. G. Vallauri - Fossano
 
ODP
Patterns in Python
dn
 
ODP
Drupal development
Dennis Povshedny
 
PPTX
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Steven Pignataro
 
ODP
Best practices tekx
Lorna Mitchell
 
PDF
Language-agnostic data analysis workflows and reproducible research
Andrew Lowe
 
PPTX
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Malla Reddy University
 
PPTX
Presentation c++
JosephAlex21
 
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
James Titcumb
 
PPT
Visual Studio .NET2010
Satish Verma
 
PDF
Open Source RAD with OpenERP 7.0
Quang Ngoc
 
PDF
OpenERP Technical Memento V0.7.3
Borni DHIFI
 
PPTX
Intro in understanding to C programming .pptx
abadinasargie
 
PPTX
Intro in understanding to C programming .pptx
abadinasargie
 
Smoothing Your Java with DSLs
intelliyole
 
Workshop: Symfony2 Intruduction: (Controller, Routing, Model)
Antonio Peric-Mazar
 
(1) c sharp introduction_basics_dot_net
Nico Ludwig
 
Coder Presentation Boston
Doug Green
 
Working Effectively With Legacy Perl Code
erikmsp
 
Drupal Best Practices
manugoel2003
 
The GO Language : From Beginners to Gophers
I.I.S. G. Vallauri - Fossano
 
Patterns in Python
dn
 
Drupal development
Dennis Povshedny
 
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Steven Pignataro
 
Best practices tekx
Lorna Mitchell
 
Language-agnostic data analysis workflows and reproducible research
Andrew Lowe
 
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Malla Reddy University
 
Presentation c++
JosephAlex21
 
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
James Titcumb
 
Visual Studio .NET2010
Satish Verma
 
Open Source RAD with OpenERP 7.0
Quang Ngoc
 
OpenERP Technical Memento V0.7.3
Borni DHIFI
 
Intro in understanding to C programming .pptx
abadinasargie
 
Intro in understanding to C programming .pptx
abadinasargie
 
Ad

More from megakott (8)

PPTX
Reply via-email service: hidden traps
megakott
 
ODP
Hackathon
megakott
 
ODP
Middleware
megakott
 
ODP
Perl resources
megakott
 
ODP
Piano on-perl
megakott
 
ODP
Office vs. Remote
megakott
 
ODP
Anaglyph 3D-images: trends and demo
megakott
 
ODP
Saint Perl 2009: CGI::Ajax demo
megakott
 
Reply via-email service: hidden traps
megakott
 
Hackathon
megakott
 
Middleware
megakott
 
Perl resources
megakott
 
Piano on-perl
megakott
 
Office vs. Remote
megakott
 
Anaglyph 3D-images: trends and demo
megakott
 
Saint Perl 2009: CGI::Ajax demo
megakott
 

Recently uploaded (20)

PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 

Aspect-oriented programming in Perl

  • 1. Aspect-oriented programming in Perl Speaker: Aleksandr Kotov [email_address]
  • 3. AOP is a kind of trending topic now
  • 4. Howewer, not so popular as Perl...
  • 5. Back to contents Aspect-Oriented Programming In Perl The Goal: just overview of AOP :-)
  • 6. So, paradigm Aspect-oriented programming is a modern trending paradigm
  • 10. Wikipedia In computing, aspect-oriented programming (AOP) is a programming paradigm which isolates secondary or supporting functions from the main program's business logic.
  • 11. 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.
  • 12. 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.
  • 13. 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
  • 14. Motivation ...That means to change logging can require modifying all affected modules...
  • 15. 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.
  • 17. 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
  • 18. 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.
  • 19. Terminology Join Point Pointcut Advice Advice Code Weave The Aspect
  • 20. Join Point An event that occurs during the running of a program. Currently only calls to subroutines are recognized as join points.
  • 21. 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.
  • 22. Advice A pointcut, with code that will run when it matches. The code can be run before or after the matched sub is run.
  • 23. 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.
  • 24. 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.
  • 25. The Aspect An object that installs advice. A way to package advice and other Perl code, so that it is reusable.
  • 26. 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.
  • 27. 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.
  • 28. 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.
  • 29. 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.
  • 30. 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.
  • 31. 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.
  • 32. 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.
  • 33. 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+$/;
  • 34. Advice after { print "Person::get_address has returned!\n"; } call 'Person::get_address';
  • 35. 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';
  • 36. The whole example (1 / 4) package Person; sub create { # ... } sub set_name { # ... } sub get_address { # ... }
  • 37. 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_/;
  • 38. 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;
  • 39. 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';
  • 40. Usage (in blogs) http://use.perl.org/ - blogs about Perl http://use.perl.org/~unimatrix/journal/857 http://use.perl.org/~Alias/journal/40368
  • 41. Thanks! Questions?! This presentation will be uploaded into «slideshare» within a week.