The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Beam::Emitter - Role for event emitting classes

VERSION

version 0.003

SYNOPSIS

    package My::Emitter;

    use Moo;
    with 'Beam::Emitter';

    sub do_something {
        my ( $self ) = @_;

        # Give event listeners a chance to prevent something
        my $event = $self->emit( "before_something" );
        return if $event->is_default_stopped;

        # ... do something

        # Notify listeners we're done with something
        $self->emit( 'after_something' );
    }

DESCRIPTION

This role is used by classes that want to emit events to subscribers.

ATTRIBUTES

_listeners

The event listeners registered on this object.

METHODS

subscribe ( event_name, subref )

Subscribe to an event from this object. event_name is the name of the event. subref is a subroutine reference that takes a single argument, the Beam::Event that is being emitted.

on ( event_name, subref )

Alias for "subscribe".

unsubscribe ( event_name [, subref ] )

Unsubscribe from an event. event_name is the name of the event. subref is the single listener subref to be removed. If no subref is given, will remove all listeners for this event.

un ( event_name [, subref ] )

An alias for "unsubscribe"

emit ( name, event_args )

Emit a Beam::Event with the given name. event_args is a list of name => value pairs to give to the Beam::Event object.

Use the class key in event_args to specify a different Event class.

emit_args ( name, callback_args )

Emit an event with the given name. callback_args is a list that will be given directly to each subscribed callback.

Use this to completely avoid using Beam::Event completely.

AUTHOR

Doug Bell <preaction@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Doug Bell.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.