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

PPTX
Python Programming Basics for begginners
Abishek Purushothaman
 
PPT
Functions
PatriciaPabalan
 
PDF
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
PPT
Functions in c++
Maaz Hasan
 
PPTX
C functions
University of Potsdam
 
PPTX
Why functional programming in C# & F#
Riccardo Terrell
 
PPTX
Functions in C - Programming
GaurangVishnoi
 
PPTX
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
reactima
 
Python Programming Basics for begginners
Abishek Purushothaman
 
Functions
PatriciaPabalan
 
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
Functions in c++
Maaz Hasan
 
Why functional programming in C# & F#
Riccardo Terrell
 
Functions in C - Programming
GaurangVishnoi
 
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
reactima
 

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
 
Ad

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
Aspect Oriented Software Development
Jignesh Patel
 
PDF
AOP
Joshua Yoon
 
ODP
Weaving aspects in PHP with the help of Go! AOP library
Alexander Lisachenko
 
PPTX
Aspect Oriented Programming
Shreya Chatterjee
 
PPTX
spring aop
Kalyani Patil
 
ODP
Aspect-Oriented Programming for PHP
William Candillon
 
PPTX
Aspect Oriented Programming
Rajesh Ganesan
 
PPTX
Spring AOP
Radhakrishna Mutthoju
 
PDF
Spring aop
Hamid Ghorbani
 
PPTX
Introduction to Aspect Oriented Programming
Amir Kost
 
PPTX
Spring aop concepts
RushiBShah
 
PPTX
Aspect Oriented Programming
Fernando Almeida
 
PDF
Aspect oriented software development
Maryam Malekzad
 
PDF
AspectC++: Language Proposal and Prototype Implementation
dinomasch
 
PPTX
Spring AOP Introduction
b0ris_1
 
PDF
Working Effectively With Legacy Perl Code
erikmsp
 
PPT
ASPECT ORIENTED PROGRAMING(aop)
kvsrteja
 
PPTX
Spring AOP in Nutshell
Onkar Deshpande
 
PDF
SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ
Ted Leung
 
PPTX
Spring aop
sanskriti agarwal
 
Aspect Oriented Software Development
Jignesh Patel
 
Weaving aspects in PHP with the help of Go! AOP library
Alexander Lisachenko
 
Aspect Oriented Programming
Shreya Chatterjee
 
spring aop
Kalyani Patil
 
Aspect-Oriented Programming for PHP
William Candillon
 
Aspect Oriented Programming
Rajesh Ganesan
 
Spring aop
Hamid Ghorbani
 
Introduction to Aspect Oriented Programming
Amir Kost
 
Spring aop concepts
RushiBShah
 
Aspect Oriented Programming
Fernando Almeida
 
Aspect oriented software development
Maryam Malekzad
 
AspectC++: Language Proposal and Prototype Implementation
dinomasch
 
Spring AOP Introduction
b0ris_1
 
Working Effectively With Legacy Perl Code
erikmsp
 
ASPECT ORIENTED PROGRAMING(aop)
kvsrteja
 
Spring AOP in Nutshell
Onkar Deshpande
 
SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ
Ted Leung
 
Spring aop
sanskriti agarwal
 

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
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
PDF
Chapter 1 Introduction to CV and IP Lecture Note.pdf
Getnet Tigabie Askale -(GM)
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
PDF
Software Development Company | KodekX
KodekX
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PDF
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Why Your AI & Cybersecurity Hiring Still Misses the Mark in 2025
Virtual Employee Pvt. Ltd.
 
Chapter 1 Introduction to CV and IP Lecture Note.pdf
Getnet Tigabie Askale -(GM)
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
The Evolution of KM Roles (Presented at Knowledge Summit Dublin 2025)
Enterprise Knowledge
 
Software Development Company | KodekX
KodekX
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
This slide provides an overview Technology
mineshkharadi333
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 

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.