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

Message::Stack - Deal with a "stack" of messages

SYNOPSIS

  my $stack = Message::Stack->new;

  $stack->add(Message::Stack::Message->new(
      id        => 'something_happened',
      level     => 'error',
      scope     => 'login_form',
      subject   => 'username',
      text      => 'Something happened!'
  ));
  # Or... for those that want to type less
  $stack->add({
      id        => 'something_else_happened',
      level     => 'error',
      scope     => 'login_form',
      subject   => 'password',
      text      => 'Something else happened!'
  });
  
  ...
  my $errors = $stack->for_level($error);
  # Or
  my $login_form_errors = $stack->for_scope('login_form');
  $login_form_errors->for_id('username');
  print "Username has ".$login_form_errors->count." errors.\n";

DESCRIPTION

Message::Stack provides a mechanism for storing messages until they can be consumed. A stack is used to retain order of occurrence. Each message may have a id, level, scope, subject and text. Consult the documentation for Message::Stack::Message for an explanation of these attributes.

This is not a logging mechanism. The original use was to store various errors or messages that occur during processing for later display in a web application. The messages are added via add.

SERIALIZATION

This module uses MooseX::Storage::Deferred to facilitate easy serialization. Consult the documentation for MooseX::Storage::Deferred options, but the gist is:

  my $json = $stack->freeze({ format => 'JSON' });
  ...
  my $stack = Message::Stack->thaw($json, { format => 'JSON' });

METHODS

add ($message)

Adds the supplied message to the stack. $message may be either a Message::Stack::Message object or a hashref with similar keys.

count

Returns the number of messages in the stack.

messages

Returns the full arrayref of messages for this stack.

first_message

Returns the first message (if there is one, else undef)

search (CODEREF)

Returns a Message::Stack containing messages that return true when passed to the coderef argument.

  $stack->search( sub { $_[0]->id eq 'someid' } )

get_message ($index)

Get the message at the supplied index.

for_id ($id)

Returns a new Message::Stack containing only the message objects with the supplied id. If there are no messages for that level then the stack returned will have no messages.

for_level ($level)

Returns a new Message::Stack containing only the message objects with the supplied level. If there are no messages for that level then the stack returned will have no messages.

for_scope ($scope)

Returns a new Message::Stack containing only the message objects with the supplied scope. If there are no messages for that scope then the stack returned will have no messages.

for_subject ($subject)

Returns a new Message::Stack containing only the message objects with the supplied subject. If there are no messages for that subject then the stack returned will have no messages.

has_messages

Returns true if there are messages in the stack, else false

has_id ($id)

Returns true if there are messages with the supplied id.

has_level ($level)

Returns true if there are messages with the supplied level.

has_scope ($scope)

Returns true if there are messages with the supplied scope.

has_subject ($subject)

Returns true if there are messages with the supplied subject.

last_message

Returns the last message (if there is one, else undef)

reset

Clear all messages, resetting this stack.

AUTHOR

Cory G Watson, <gphat at cpan.org>

CONTRIBUTORS

Jay Shirley

Stevan Little

Justin Hunter

Jon Wright

Mike Eldridge

COPYRIGHT & LICENSE

Copyright 2010 Cory G Watson, all rights reserved.

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