Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages


home | help
Class::Adapter::Clear(User Contributed Perl DocumentatClass::Adapter::Clear(3)

       Class::Adapter::Clear - A handy base Adapter class that makes no

       Hello World with the normal way

	 # Load	and create the CGI
	 use CGI;
	 $q = new CGI;

	 # Create the page
	 print $q->header,		      #	HTTP Header
	       $q->start_html('hello world'), #	Start the page
	       $q->h1('hello world'),	      #	Hello World!
	       $q->end_html;		      #	End the	page

       Hello World with the Adapter'ed way

	 # Load	and create the CGI
	 use CGI;
	 $q = new CGI;

	 # Convert to an Adapter
	 use Class::Adapter::Clear;
	 $q = new Class::Adapter::Clear( $q );

	 # Create the page
	 print $q->header,		      #	HTTP Header
	       $q->start_html('hello world'), #	Start the page
	       $q->h1('hello world'),	      #	Hello World!
	       $q->end_html;		      #	End the	page

       Creating	a CGI Adapter class using Class::Adapter::Clear

	 package My::CGI;

	 use base 'Class::Adapter::Clear';

	 # Optional - Create the thing we are decorating auto-magically
	 sub new {
	     my	$class = shift;

	     # Create the object we are	decorating
	     my	$query = CGI->new(@_);

	     # Wrap it in the Adapter

	 # Decorate the	h1 method to change what is created
	 sub h1	{
	       my $self	= shift;
	       my $str	= shift;

	   # Do	something before the real method call
	   if (	defined	$str and $str eq 'hello	world' ) {
	       $str = 'Hello World!';

	   $self->_OBJECT_->($str, @_);

       "Class::Adapter::Clear" provides	the base class for creating one	common
       type of Class::Adapter classes. For more	power, move up to

       On it's own "Class::Adapter::Clear" passes all methods through to the
       same method in the parent object	with the same parameters, responds to
       "->isa" like the	parent object, and responds to "->can" like the	parent

       It looks	like a "Duck", and it quacks like a "Duck".

       On this base, you simple	implement whatever method you want to do
       something special to.

	 # Different method, same parameters
	 sub method1 {
	     my	$self =	shift;
	     $self->_OBJECT_->method2(@_); # Call a different method

	 # Same	method,	different parameters
	 sub method1 {
	     my	$self =	shift;
	     $self->_OBJECT_->method1( lc($_[0]) ); # Lowercase	the param

	 # Same	method,	same parameters, tweak the result
	 sub method1 {
	     my	$self =	shift;
	     my	$rv = $self->_OBJECT_->method1(@_);
	     $rv =~ s/\n/<br>\n/g; # Add line-break HTML tags at each newline
	     return $rv;

       As you can see, the advantage of	this full-scale	Adapter	approach,
       compared	to inheritance,	or function wrapping (see Class::Hook),	is
       that you	have complete and utter	freedom	to do anything you might need
       to do, without stressing	the Perl inheritance model or doing anything
       unusual or tricky with "CODE" references.

       You may never need this much power. But when you	need it, you really
       need it.

       As an aside, Class::Adapter::Clear is implemented with the following
       Class::Adapter::Builder formula.

	 use Class::Adapter::Builder
	     ISA      => '_OBJECT_',
	     AUTOLOAD => 1;

   new $object
       As does the base	Class::Adapter class, the default "new"	constructor
       takes a single object as	argument and creates a new object which	holds
       the passed object.

       Returns a new "Class::Adapter::Clear" object, or	"undef"	if you do not
       pass in an object.

       Bugs should be reported via the CPAN bug	tracker	at


       For other issues, contact the author.

       Adam Kennedy <>

       Class::Adapter, Class::Adapter::Builder

       Copyright 2005 -	2011 Adam Kennedy.

       This program is free software; you can redistribute it and/or modify it
       under the same terms as Perl itself.

       The full	text of	the license can	be found in the	LICENSE	file included
       with this module.

perl v5.32.1			  2011-03-24	      Class::Adapter::Clear(3)


Want to link to this manual page? Use this URL:

home | help