Aspect

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

NAME
    Aspect - Aspect-Oriented Programming (AOP) for Perl

SYNOPSIS
      use Aspect;
      
  # Run some code "Advice" before a particular function
      before {
          print "About to call create\n";
      } call 'Person::create';
      
  # Run Advice after several methods and hijack their return values
      after {
          print "Called getter/setter " . $_->sub_name . "\n";
          $_->return_value(undef);
      } call qr/^Person::[gs]et_/;
      
  # Run Advice conditionally based on multiple factors
      before {
          print "Calling a get method in void context within Tester::run_tests";
      } wantvoid
      & ( call qr/^Person::get_/ & ! call 'Person::get_not_trapped' )
      & cflow 'Tester::run_tests';
      
  # Context-aware runtime hijacking of a method if certain 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';
      
  # Catch and handle unexpected exceptions in a function into a formal object
      after {
          $_->exception(
              Exception::Unexpected->new($_->exception)
          );
      } throwing()
      & ! throwing('Exception::Expected')
      & ! throwing('Exception::Unexpected');
      
  # Run Advice only on the outmost of a recursive series of calls
      around {
        print "Starting recursive child search\n";
        $_->proceed;
        print "Finished recursive child search\n";
      } call 'Person::find_child' & highest;
      
  # Run Advice only during the current lexical scope
      SCOPE: {
          my $hook = before {
              print "About to call create\n";
          } call 'Person::create';
          Person->create('Bob'); # Advice will run
      }
      Person->create('Tom'); # Advice won't run
      
  # Use a pre-packaged collection "Aspect" of Advice rules to change a class
      aspect Singleton => 'Foo::new';
      
  # Define debugger breakpoints with high precision and conditionality
      aspect Breakpoint => call qr/^Foo::.+::Bar::when_/ & wantscalar & highest;

DESCRIPTION
  What is Aspect-Oriented Programming?
    Aspect-Oriented Programming (AOP) is a programming paradigm which aims
    to increase modularity by allowing the separation of "cross-cutting
    "concerns.

    It includes programming methods and tools that support the
    modularization of concerns at the level of the source code, while
    "aspect-oriented software development" refers to a whole engineering
    discipline.

    Aspect-Oriented Programming (AOP) allows you to modularise code for
    issues that would otherwise be spread across many parts of a program and
    be problematic to both implement and maintain.



( run in 1.272 second using v1.01-cache-2.11-cpan-39bf76dae61 )