
Class::Context - simple implementation of Context Object pattern

Simple usage
use Class::Context;
my $context = Class::Context->new();
$context->setExample('exampledata');
print $context->getExample();
# exampledata
$context->setExample2({ some => 'hash'});
print ref($context->getExample2());
# HASH
strict usage
my $context = Class::Context->new( strict => ['example']);
$context->setExample('exampledata');
print $context->getExample() . "\n";
# exampledata
$context->setExample2('die,hacker!');
print ref($context->getExample2()) . "\n";
# died with 'unknown param' message
basic method syntax
(get|set) + ucfirst(lc(paramname))

This module does not generate methods on-the-fly and does not use "eval()", "no strict" or other dirty tricks.

You can add more functionality by extending Class::Context.
Class::Context provides two basic operations - 'get' and 'set'. You can add or redefine it.
package Mycontext;
use strict;
use base 'Class::Context';
sub _defineCustomOperations
{
my $self = shift;
# $self->{operations} is a hashref, where related functions stored
# $self->{registry} is a hashref, where params stored
# add new operation
$self->{operations}->{load} = sub {
my ($param) = @_;
my $object = Some::Factory->create($param);
$self->{registry}->{$param} = $object;
};
# redefine operation get.
# add ondemand object loading
$self->{operations}->{get} = sub {
my ($param) = @_;
if($self->{registry}->{$param}){
return $self->{registry}->{$param};
}else{
$self->{operations}->{load}->($param);
return $self->{registry}->{$param};
}
return 1;
};
# now if you invoke
# $context->getRequest;
# new object will be stored in $context->{registry}->{request}
# or returned if it already exists
return 1;
}
Also you can redefine default exception method for more useful error handling sub _die { my $self = shift; my ($message) = @_;
throw Error::Simple($message);
}

You can find a helpful information by using Google.
Keywords: 'Context Object Pattern', 'agile software development', 'software design patterns', 'Patterns of Enterprise Application Architecture'.

cmapuk[0nline], cmapuk.0nline@gmail.com

Copyright (C) 2009 by cmapuk[0nline]
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.