Class:...::Clear(3pm)

πŸ“¦
version 1.09 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

okay bestie, sit down, hydrate, put your phone on do-not-disturb 🫢 because we're reading a whole manpage together and honestly? it's a cute one. This is Class::Adapter::Clear, from the User Contributed Perl Documentation, section 3pm. It's giving "I change literally nothing and I do it flawlessly." πŸ‘‘


πŸ’– NAME πŸ’–

Class::Adapter::Clear β€” a handy base Adapter class that makes no changes ✨

🌸 VERSION 🌸

version 1.09


πŸŽ€ SYNOPSIS πŸŽ€

First flavor: Hello World with CGI.pm 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

Now the glow-up: Hello World with CGI.pm the Adapter'ed way πŸ’… (same everything, but wrapped)

# 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

And now the main character move β€” 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
    $class->SUPER::new($query);
}

# 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, @_);
}

πŸ¦‹ DESCRIPTION πŸ¦‹

So Class::Adapter::Clear hands you the base class for making one very common type of Class::Adapter class. Want more power? Level up to Class::Adapter::Builder, no cap πŸ”₯

On its 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 object.

It looks like a Duck, and it quacks like a Duck. πŸ¦† iconic behavior, truly.

On top of that base, you simply implement whatever method you want to do something special to. Like:

# 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;
}

See what's happening? πŸ‘€ The advantage of this full-scale Adapter approach, compared to inheritance, or function wrapping (peep Class::Hook), is that you have complete and utter freedom to do anything you might need to do β€” without stressing the Perl inheritance model, and without doing anything unusual or tricky with CODE references. periodt.

You may never need this much power. But when you need it, you really need it. πŸ’…

As an aside (because we love a lil behind-the-scenes moment 🎬), Class::Adapter::Clear is implemented with the following Class::Adapter::Builder formula:

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

🧚 METHODS 🧚

✨ new $object

Same as the base Class::Adapter class does it: 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. (so like, actually pass one in, bestie 😭)


🌈 SEE ALSO 🌈

  • Class::Adapter
  • Class::Adapter::Builder

πŸ’œ SUPPORT πŸ’œ

Bugs may be submitted through the RT bug tracker https://rt.cpan.org/Public/Dist/Display.html?Name=Class-Adapter (or bug-Class-Adapter@rt.cpan.org). Found a bug? Tell on it, respectfully πŸ›


πŸ‘‘ AUTHOR πŸ‘‘

Adam Kennedy <adamk@cpan.org>


This software is copyright (c) 2005 by Adam Kennedy.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. (fr fr, go wild, just keep the terms πŸ’–)


okay that's the whole page, you ate that πŸ“ no notes, go forth and adapt πŸ’…

and that's a wrap on this one, babe β€” it came to you via perl v5.36.0, dated 2022-12-04, and yeah, the manual name is still Class:...::Clear(3pm) because some things never change (unlike this class, which is literally built on not changing πŸ’€). mwah. πŸŽ€

πŸ“¦ where this page comes from