Simran::Base - This is the base class for all modules in the Simran:: area.


Simran documentation Contained in the Simran distribution.

Index


Code Index:

NAME

Top

Simran::Base - This is the base class for all modules in the Simran:: area.

SYNOPSIS

Top

 use base qw(Simran::Base);

DESCRIPTION

Top

This class should never be instantiated directly.

You should only be inheriting from it.

METHODS

Top

new

Description
 Provide a standard "new" method for all classes that inherit from this class.

Input
  * 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

Return
 * An Object - if all was well
   undef     - otherwise

Method Type
 * This method should be used as a class method only

setError

Description

This method sets the error to the message that you provide for the calling module.

Input
 * @errors   - Error Messages

Return
 * undef - This function returns undef so you can do things like:

           return $self->setError("No User Specified");

           in your code. 

Note
 * Sets the variable Your::Class::Name::_ErrorMessages although this variable
   should never be used directly. 

Method Type
 * This method can be used as a class or object method

getError

Description

This method returns the last error message for the calling module...

Input
 * None

Return
 * @errors   - Errors as set by setError

Method Type
 * This method can be used as a class or object method

strip

Description

Strips out leading and training whitespaces from references...

Input
  * Reference to an array, hash or string
    or
    A string

Return
  * If input was a reference then, None - The reference passed as input is modified... 
    Else, the stripped string

Method Type
 * This method can be used as a class or object method

AUTHOR

Top

Simran, <simran@cse.unsw.edu.au>

COPYRIGHT AND LICENSE

Top


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;