GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Venus::Role::Subscribable(3) User Contributed Perl Documentation Venus::Role::Subscribable(3)

Venus::Role::Subscribable - Subscribable Role

Subscribable Role for Perl 5

  package Example;
  use Venus::Class;
  with 'Venus::Role::Subscribable';
  sub execute {
    $_[0]->publish('on.execute');
  }
  package main;
  my $example = Example->new;
  # $example->subscribe('on.execute', sub{...});
  # bless(..., 'Example')
  # $example->publish('on.execute');
  # bless(..., 'Example')

This package provides a mechanism for publishing and subscribing to events.

This package provides the following methods:

  publish(string $name, any @args) (object)

The publish method notifies all subscribers for a given event and returns the invocant.

Since 1.75

publish example 1
  # given: synopsis
  package main;
  $example = $example->publish;
  # bless(..., 'Example')
    
publish example 2
  # given: synopsis
  package main;
  $example = $example->publish('on.execute');
  # bless(..., 'Example')
    
publish example 3
  # given: synopsis
  package main;
  $example->subscribe('on.execute', sub {$example->{emitted} = [@_]});
  $example = $example->publish('on.execute');
  # bless(..., 'Example')
    
publish example 4
  # given: synopsis
  package main;
  $example->subscribe('on.execute', sub {$example->{emitted} = [@_]});
  $example = $example->publish('on.execute', [1..4]);
  # bless(..., 'Example')
    

  subscribe(string $name, coderef $code) (object)

The subscribe method registers a subscribers (i.e. callbacks) for a given event, and returns the invocant.

Since 1.75

subscribe example 1
  # given: synopsis
  package main;
  $example = $example->subscribe('on.execute', sub {$example->{emitted} = [@_]});
  # bless(..., 'Example')
    
subscribe example 2
  # given: synopsis
  package main;
  $example = $example->subscribe('on.execute', sub {$example->{emitted_1} = [@_]});
  # bless(..., 'Example')
  $example = $example->subscribe('on.execute', sub {$example->{emitted_2} = [@_]});
  # bless(..., 'Example')
  $example = $example->subscribe('on.execute', sub {$example->{emitted_3} = [@_]});
  # bless(..., 'Example')
  # $example->publish('on.execute');
  # bless(..., 'Example')
    

  subscribers(string $name) (number)

The subscribers method returns the number of subscribers (i.e. callbacks) for a given event.

Since 1.75

subscribers example 1
  # given: synopsis
  package main;
  $example = $example->subscribers;
  # 0
    
subscribers example 2
  # given: synopsis
  package main;
  $example = $example->subscribers('on.execute');
  # 0
    
subscribers example 3
  # given: synopsis
  package main;
  $example = $example->subscribe('on.execute', sub {$example->{emitted_1} = [@_]});
  $example = $example->subscribe('on.execute', sub {$example->{emitted_2} = [@_]});
  $example = $example->subscribe('on.execute', sub {$example->{emitted_3} = [@_]});
  $example = $example->subscribers('on.execute');
  # 3
    

  unsubscribe(string $name, coderef $code) (object)

The unsubscribe method deregisters all subscribers (i.e. callbacks) for a given event, or a specific callback if provided, and returns the invocant.

Since 1.75

unsubscribe example 1
  # given: synopsis
  package main;
  $example = $example->unsubscribe;
  # bless(..., 'Example')
    
unsubscribe example 2
  # given: synopsis
  package main;
  $example = $example->unsubscribe('on.execute');
  # bless(..., 'Example')
    
unsubscribe example 3
  # given: synopsis
  package main;
  $example = $example->subscribe('on.execute', sub {$example->{emitted_1} = [@_]});
  $example = $example->subscribe('on.execute', sub {$example->{emitted_2} = [@_]});
  $example = $example->subscribe('on.execute', sub {$example->{emitted_3} = [@_]});
  $example = $example->unsubscribe('on.execute');
  # bless(..., 'Example')
    
unsubscribe example 4
  # given: synopsis
  package main;
  my $execute = sub {$example->{execute} = [@_]};
  $example = $example->subscribe('on.execute', $execute);
  $example = $example->unsubscribe('on.execute', $execute);
  # bless(..., 'Example')
    

Awncorp, "awncorp@cpan.org"

Copyright (C) 2022, Awncorp, "awncorp@cpan.org".

This program is free software, you can redistribute it and/or modify it under the terms of the Apache license version 2.0.

2023-11-27 perl v5.40.2

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.