Superclass for all netsds apis
package MyClass; use base 'NetSDS::Class::Abstract'; _\|_PACKAGE_\|_->mk_accessors(qw/my_field/); sub error_sub { my ($self) = @_; if (!$self->my_field) { return $self->error("No my_field defined"); } } 1;
\*(C`NetSDS::Class::Abstract\*(C' is a superclass for all other NetSDS classes, containing the following functionality:
common class constructor
safe modules inclusion
class and objects accessors
logging
error handling;
All other class/object APIs should inherit this class to use it's functionality in standard way.
\*(C`new()\*(C' method implements common constructor for NetSDS classes. Constructor may be overwriten in inherited classes and usually this happens to implement module specific functionality. Constructor requres parameters as hash that are set as object properties. my $object = NetSDS::SomeClass->new( foo => 'abc', bar => 'def', );
See Class::Accessor for details. _\|_PACKAGE_\|_->mk_class_accessors('foo', 'bar');
See Class::Accessor::Class for details. $self->mk_accessors('foo', 'bar'); Other \*(C`Class::Accessor::Class\*(C' methods available as well.
\*(C`use_modules()\*(C' provides safe on demand modules loader. It requires list of modules names as parameters Return 1 in case of success or \*(C`undef\*(C' if faied. Error messages in case of failure are available using \*(C`errstr()\*(C' call. Example: # Load modules for daemonization if ($daemon_mode) { $self->use_modules("Proc::Daemon", "Proc::PID::File"); }
Return unblessed data structure of object that may be used when some code requires non blessed structures (like \s-1JSON\s0 serialization). Example: my $var = $obj->unbless();
\*(C`logger\*(C' property is an object that should provide functionality handling log messaging. Usually it's object of NetSDS::Logger class or \*(C`undef\*(C'. However it may another object implementing non-standard features like sending log to e-mail or to \s-1DBMS\s0. Example: # Set logger and send log message $obj->logger(NetSDS::Logger->new()); $obj->log("info", "Logger connected"); Paramters: log level, log message $obj->log("info", "We still alive");
\*(C`error()\*(C' method set error message and optional error code. It can be invoked in both class and object contexts. Example 1: set class error NetSDS::Foo->error("Mistake found"); Example 2: set object error with code $obj->error("Can't launch rocket", BUG_STUPID);
\*(C`errstr()\*(C' method returns error string in both object and class contexts. Example: warn "We have an error: " . $obj->errstr;
\*(C`errcode()\*(C' method returns error code in both object and class contexts. Example: if ($obj->errcode == 42) { print "Epic fail! We've found an answer!"; }
See \*(C`samples\*(C' directory and other \*(C`NetSDS\*(C' moduleis for examples of code.
Class::Accessor::Class
Michael Bochkaryov <[email protected]>
Copyright (C) 2008-2009 Net Style Ltd.
This program is free software; you can redistribute it and/or modify it under the terms of the \s-1GNU\s0 General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but \s-1WITHOUT\s0 \s-1ANY\s0 \s-1WARRANTY\s0; without even the implied warranty of \s-1MERCHANTABILITY\s0 or \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0. See the \s-1GNU\s0 General Public License for more details.
You should have received a copy of the \s-1GNU\s0 General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, \s-1MA\s0 02111-1307 \s-1USA\s0