| Simran documentation | Contained in the Simran distribution. |
Simran::Base - This is the base class for all modules in the Simran:: area.
use base qw(Simran::Base);
This class should never be instantiated directly.
You should only be inheriting from it.
Provide a standard "new" method for all classes that inherit from this class.
* A HASH of name/value pairs you want to set as properties
A property can be referenced as such (after being set):
$object->{property_name};
(the properties are set via the _init method)
Note: This new method calls $self->_init(@_) and expects '1' or 'undef' depending on if the _init was successful
* An Object - if all was well undef - otherwise
* This method should be used as a class method only
This method sets the error to the message that you provide for the calling module.
* @errors - Error Messages
* undef - This function returns undef so you can do things like:
return $self->setError("No User Specified");
in your code.
* Sets the variable Your::Class::Name::_ErrorMessages although this variable should never be used directly.
* This method can be used as a class or object method
This method returns the last error message for the calling module...
* None
* @errors - Errors as set by setError
* This method can be used as a class or object method
Strips out leading and training whitespaces from references...
* Reference to an array, hash or string
or
A string
* If input was a reference then, None - The reference passed as input is modified...
Else, the stripped string
* This method can be used as a class or object method
Simran, <simran@cse.unsw.edu.au>
Copyright 2002 by simran
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Simran documentation | Contained in the Simran distribution. |
################################################################################################################## # # Source : $Source: /home/simran/cvs/cpan/Simran/Base.pm,v $ # Revision : $Revision: 1.1.1.1 $ # Modified By : $Author: simran $ # Last Modified: $Date: 2002/12/04 03:46:29 $ # ################################################################################################################## package Simran::Base; ################################################################################################################## # # load modules as required... # # use 5.008; use strict; use warnings; ################################################################################################################## # # #
################################################################################################################## # # GLOBALS # our $VERSION = '0.01'; ################################################################################################################## # # PUBLIC METHODS # #################################### # # new #
sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; bless $self, $class; $self->_init(@_) || return $self->setError("Could not initlalise object: ".$self->getError()); return $self; } #################################### # # setError #
sub setError { my $self = shift; my $class = ref($self) || $self; my @error = @_; { no strict qw(refs); ${$class.'::_ErrorMessages'} = \@error; use strict; } return; } #################################### # # getError #
sub getError { my $self = shift; my $class = ref($self) || $self; my $error; { no strict qw(refs); $error = ${$class.'::_ErrorMessages'}; use strict; } if ($error && ref($error)) { return @$error; } else { return $error; } } ############################################ # strip #
sub strip { my $self = shift; my $ref = shift; if (! ref($ref)) { $ref =~ s/(^[\s\t]*)|([\s\t]*$)//g; return $ref; } elsif (ref($ref) =~ /^ARRAY$/i) { foreach my $i (0 .. $#$ref) { $ref->[$i] =~ s/(^[\s\t]*)|([\s\t]*$)//g; } } elsif (ref($ref) =~ /^HASH$/i) { while (my ($key, $value) = each %$ref) { delete $ref->{$key}; $key =~ s/(^[\s\t]*)|([\s\t]*$)//g; $value =~ s/(^[\s\t]*)|([\s\t]*$)//g; $ref->{$key} = $value; } } elsif (ref($ref) =~ /^SCALAR$/i) { $$ref =~ s/(^[\s\t]*)|([\s\t]*$)//g; } else { die "Unknown reference type"; } } ################################################################################################################## # # PRIVATE METHODS # #################################### # _init # # Initialize the class # # Input: A Hash # eg. $self->_init(carp=>1, logfile=>"/tmp/log.txt") # Note: All properties passed are set # # Return Values: 1 # # sub _init { my $self = shift; my %properties = @_; foreach (keys %properties) { my $property = $_; $self->{$property} = $properties{$_}; } return 1; } 1;