1Class::Adapter::Clear(3U)ser Contributed Perl DocumentatiColnass::Adapter::Clear(3)
2
3
4
6 Class::Adapter::Clear - A handy base Adapter class that makes no
7 changes
8
10 version 1.09
11
13 Hello World with CGI.pm the normal way
14
15 # Load and create the CGI
16 use CGI;
17 $q = new CGI;
18
19 # Create the page
20 print $q->header, # HTTP Header
21 $q->start_html('hello world'), # Start the page
22 $q->h1('hello world'), # Hello World!
23 $q->end_html; # End the page
24
25 Hello World with CGI.pm the Adapter'ed way
26
27 # Load and create the CGI
28 use CGI;
29 $q = new CGI;
30
31 # Convert to an Adapter
32 use Class::Adapter::Clear;
33 $q = new Class::Adapter::Clear( $q );
34
35 # Create the page
36 print $q->header, # HTTP Header
37 $q->start_html('hello world'), # Start the page
38 $q->h1('hello world'), # Hello World!
39 $q->end_html; # End the page
40
41 Creating a CGI Adapter class using Class::Adapter::Clear
42
43 package My::CGI;
44
45 use base 'Class::Adapter::Clear';
46
47 # Optional - Create the thing we are decorating auto-magically
48 sub new {
49 my $class = shift;
50
51 # Create the object we are decorating
52 my $query = CGI->new(@_);
53
54 # Wrap it in the Adapter
55 $class->SUPER::new($query);
56 }
57
58 # Decorate the h1 method to change what is created
59 sub h1 {
60 my $self = shift;
61 my $str = shift;
62
63 # Do something before the real method call
64 if ( defined $str and $str eq 'hello world' ) {
65 $str = 'Hello World!';
66 }
67
68 $self->_OBJECT_->($str, @_);
69 }
70
72 "Class::Adapter::Clear" provides the base class for creating one common
73 type of Class::Adapter classes. For more power, move up to
74 Class::Adapter::Builder.
75
76 On it's own "Class::Adapter::Clear" passes all methods through to the
77 same method in the parent object with the same parameters, responds to
78 "->isa" like the parent object, and responds to "->can" like the parent
79 object.
80
81 It looks like a "Duck", and it quacks like a "Duck".
82
83 On this base, you simple implement whatever method you want to do
84 something special to.
85
86 # Different method, same parameters
87 sub method1 {
88 my $self = shift;
89 $self->_OBJECT_->method2(@_); # Call a different method
90 }
91
92 # Same method, different parameters
93 sub method1 {
94 my $self = shift;
95 $self->_OBJECT_->method1( lc($_[0]) ); # Lowercase the param
96 }
97
98 # Same method, same parameters, tweak the result
99 sub method1 {
100 my $self = shift;
101 my $rv = $self->_OBJECT_->method1(@_);
102 $rv =~ s/\n/<br>\n/g; # Add line-break HTML tags at each newline
103 return $rv;
104 }
105
106 As you can see, the advantage of this full-scale Adapter approach,
107 compared to inheritance, or function wrapping (see Class::Hook), is
108 that you have complete and utter freedom to do anything you might need
109 to do, without stressing the Perl inheritance model or doing anything
110 unusual or tricky with "CODE" references.
111
112 You may never need this much power. But when you need it, you really
113 need it.
114
115 As an aside, Class::Adapter::Clear is implemented with the following
116 Class::Adapter::Builder formula.
117
118 use Class::Adapter::Builder
119 ISA => '_OBJECT_',
120 AUTOLOAD => 1;
121
123 new $object
124 As does the base Class::Adapter class, the default "new" constructor
125 takes a single object as argument and creates a new object which holds
126 the passed object.
127
128 Returns a new "Class::Adapter::Clear" object, or "undef" if you do not
129 pass in an object.
130
132 Class::Adapter, Class::Adapter::Builder
133
135 Bugs may be submitted through the RT bug tracker
136 <https://rt.cpan.org/Public/Dist/Display.html?Name=Class-Adapter> (or
137 bug-Class-Adapter@rt.cpan.org <mailto:bug-Class-Adapter@rt.cpan.org>).
138
140 Adam Kennedy <adamk@cpan.org>
141
143 This software is copyright (c) 2005 by Adam Kennedy.
144
145 This is free software; you can redistribute it and/or modify it under
146 the same terms as the Perl 5 programming language system itself.
147
148
149
150perl v5.38.0 2023-07-20 Class::Adapter::Clear(3)