lib/Plack/Middleware/EnvObject.pm


package Plack::Middleware::EnvObject;
use Modern::Perl '2012';
use parent qw(Plack::Middleware);
use Plack::Util::Accessor qw(name object);

# This is a simple Plack middleware to allow us to add an arbitrary object
# to the environment. It should be called with parameters 'name' and 'object'

sub call {
    my ( $self, $env ) = @_;
    my $name   = $self->name;
    my $object = $self->object;
    if ( !defined($name) )   { die("No name passed to EnvObject.\n"); }
    if ( !defined($object) ) { die("No object passed to EnvObject.\n"); }

    $env->{$name} = $object;

    return $self->app->($env);
}

1;