Module::New::Queue - Module::New::Queue documentation


Module-New documentation Contained in the Module-New distribution.

Index


Code Index:

NAME

Top

Module::New::Queue

SYNOPSIS

Top

  use Module::New::Queue;

  Module::New::Queue->register(sub { print "global\n" });
  Module::New::Queue->localize(sub {
    Module::New::Queue->register(sub { print "local\n" });
    Module::New::Queue->consume(@args); # consume local queue
  });
  Module::New::Queue->consume(@args); # consume global queue

DESCRIPTION

Top

Used internally to register commands.

METHODS

Top

localize

runs a code reference with a localized queue.

register

register a code reference to the queue.

consume

consumes the code references in the queue.

queue

returns an array of the registered code references.

clear

clears the queue.

AUTHOR

Top

Kenichi Ishigaki, <ishigaki@cpan.org>

COPYRIGHT AND LICENSE

Top


Module-New documentation Contained in the Module-New distribution.

package Module::New::Queue;

use strict;
use warnings;

our $QUEUE = [];

sub localize {
  my ($class, $code) = @_;

  local $QUEUE = [];
  $code->();
}

sub register {
  my ($class, $code) = @_;

  push @{ $QUEUE }, $code;
}

sub consume {
  my ($class, @args) = @_;

  while ( my $func = shift @{ $QUEUE } ) {
    $func->( @args );
  }
}

sub queue { @{ $QUEUE } }
sub clear { $QUEUE = [] }

1;

__END__