Class::Printable - A base class for Printable objects


Class-Printable documentation Contained in the Class-Printable distribution.

Index


Code Index:

NAME

Top

Class::Printable - A base class for Printable objects

SYNOPSIS

Top

  package MyObject;
  our @ISA = ('Class::Printable');

DESCRIPTION

Top

Sometimes it is nice to have your objects return something a bit more complex then MyObject=HASH(0x3bac438) when you print them. This module is a base class for adding that capability to your objects. This is a very simple class which when used as a base does not actually change the normal perl stringification behavior unless you override the classes toString method. Basically, it is there if you need it, and silent otherwise.

METHODS

Top

toString

This implementation actually just calls stringValue so that your object will still retain the normal perl stringification behavior. However, if your subclass overrides this method, then the return value of it will be used whenever perl needs to stringify your object.

stringValue

This will return the unmolested stringification of your perl object.

OPERATORS

Top

""

This operator, the stringification operator, is implemented with toString.

BUGS

Top

None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it.

CODE COVERAGE

Top

I use Devel::Cover to test the code coverage of my tests, below is the Devel::Cover report on this module test suite.

 ------------------------ ------ ------ ------ ------ ------ ------ ------
 File                       stmt branch   cond    sub    pod   time  total
 ------------------------ ------ ------ ------ ------ ------ ------ ------
 Class/Printable.pm        100.0    n/a    n/a  100.0  100.0  100.0  100.0
 ------------------------ ------ ------ ------ ------ ------ ------ ------
 Total                     100.0    n/a    n/a  100.0  100.0  100.0  100.0
 ------------------------ ------ ------ ------ ------ ------ ------ ------

SEE ALSO

Top

This class is very simple, and is really nothing more than a wrapper around the stringification operator capabilities of overload.

AUTHOR

Top

stevan little, <stevan@iinteractive.com>

COPYRIGHT AND LICENSE

Top


Class-Printable documentation Contained in the Class-Printable distribution.

package Class::Printable;

use strict;
use warnings;

our $VERSION = '0.02';

## overload operator
use overload q|""| => "toString", fallback => 1;

### methods

# this is the method to be overloaded
sub toString {
	my ($self) = @_;
	return $self->stringValue();
}


# return the unmolested object string
sub stringValue {
	my ($self) = @_;
	return overload::StrVal($self);
}

1;

__END__