summaryrefslogtreecommitdiff
path: root/pod/perlbot.pod
blob: fb94560948ec461a6af21c5050f6fb7184cab532 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
=head1 NAME

perlbot - Bag'o Object Tricks For Perl5 (the BOT)

=head1 INTRODUCTION

The following collection of tricks and hints is intended to whet curious
appetites about such things as the use of instance variables and the
mechanics of object and class relationships.  The reader is encouraged to
consult relevant textbooks for discussion of Object Oriented definitions and
methodology.  This is not intended as a comprehensive guide to Perl5's
object oriented features, nor should it be construed as a style guide.

The Perl motto still holds:  There's more than one way to do it.

=head1 INSTANCE VARIABLES

An anonymous array or anonymous hash can be used to hold instance
variables.  Named parameters are also demonstrated.

	package Foo;

	sub new {
		my $type = shift;
		my %params = @_;
		my $self = {};
		$self->{'High'} = $params{'High'};
		$self->{'Low'}  = $params{'Low'};
		bless $self;
	}


	package Bar;

	sub new {
		my $type = shift;
		my %params = @_;
		my $self = [];
		$self->[0] = $params{'Left'};
		$self->[1] = $params{'Right'};
		bless $self;
	}

	package main;

	$a = new Foo ( 'High' => 42, 'Low' => 11 );
	print "High=$a->{'High'}\n";
	print "Low=$a->{'Low'}\n";

	$b = new Bar ( 'Left' => 78, 'Right' => 40 );
	print "Left=$b->[0]\n";
	print "Right=$b->[1]\n";


=head1 SCALAR INSTANCE VARIABLES

An anonymous scalar can be used when only one instance variable is needed.

	package Foo;

	sub new {
		my $type = shift;
		my $self;
		$self = shift;
		bless \$self;
	}

	package main;

	$a = new Foo 42;
	print "a=$$a\n";


=head1 INSTANCE VARIABLE INHERITANCE

This example demonstrates how one might inherit instance variables from a
superclass for inclusion in the new class.  This requires calling the
superclass's constructor and adding one's own instance variables to the new
object.

	package Bar;

	sub new {
		my $self = {};
		$self->{'buz'} = 42;
		bless $self;
	}

	package Foo;
	@ISA = qw( Bar );

	sub new {
		my $self = new Bar;
		$self->{'biz'} = 11;
		bless $self;
	}

	package main;

	$a = new Foo;
	print "buz = ", $a->{'buz'}, "\n";
	print "biz = ", $a->{'biz'}, "\n";



=head1 OBJECT RELATIONSHIPS

The following demonstrates how one might implement "containing" and "using"
relationships between objects.

	package Bar;

	sub new {
		my $self = {};
		$self->{'buz'} = 42;
		bless $self;
	}

	package Foo;

	sub new {
		my $self = {};
		$self->{'Bar'} = new Bar ();
		$self->{'biz'} = 11;
		bless $self;
	}

	package main;

	$a = new Foo;
	print "buz = ", $a->{'Bar'}->{'buz'}, "\n";
	print "biz = ", $a->{'biz'}, "\n";



=head1 OVERRIDING SUPERCLASS METHODS

The following example demonstrates how one might override a superclass
method and then call the method after it has been overridden.  The
Foo::Inherit class allows the programmer to call an overridden superclass
method without actually knowing where that method is defined.


	package Buz;
	sub goo { print "here's the goo\n" }

	package Bar; @ISA = qw( Buz );
	sub google { print "google here\n" }

	package Baz;
	sub mumble { print "mumbling\n" }

	package Foo;
	@ISA = qw( Bar Baz );
	@Foo::Inherit::ISA = @ISA;  # Access to overridden methods.

	sub new { bless [] }
	sub grr { print "grumble\n" }
	sub goo {
		my $self = shift;
		$self->Foo::Inherit::goo();
	}
	sub mumble {
		my $self = shift;
		$self->Foo::Inherit::mumble();
	}
	sub google {
		my $self = shift;
		$self->Foo::Inherit::google();
	}

	package main;

	$foo = new Foo;
	$foo->mumble;
	$foo->grr;
	$foo->goo;
	$foo->google;


=head1 USING RELATIONSHIP WITH SDBM 

This example demonstrates an interface for the SDBM class.  This creates a
"using" relationship between the SDBM class and the new class Mydbm.

	use SDBM_File;
	use POSIX;

	package Mydbm;

	sub TIEHASH {
	    my $self = shift;
	    my $ref  = SDBM_File->new(@_);
	    bless {'dbm' => $ref};
	}
	sub FETCH {
	    my $self = shift;
	    my $ref  = $self->{'dbm'};
	    $ref->FETCH(@_);
	}
	sub STORE {
	    my $self = shift; 
	    if (defined $_[0]){
		my $ref = $self->{'dbm'};
		$ref->STORE(@_);
	    } else {
		die "Cannot STORE an undefined key in Mydbm\n";
	    }
	}

	package main;

	tie %foo, Mydbm, "Sdbm", O_RDWR|O_CREAT, 0640;
	$foo{'bar'} = 123;
	print "foo-bar = $foo{'bar'}\n";

	tie %bar, Mydbm, "Sdbm2", O_RDWR|O_CREAT, 0640;
	$bar{'Cathy'} = 456;
	print "bar-Cathy = $bar{'Cathy'}\n";

=head1 THINKING OF CODE REUSE

One strength of Object-Oriented languages is the ease with which old code
can use new code.  The following examples will demonstrate first how one can
hinder code reuse and then how one can promote code reuse.

This first example illustrates a class which uses a fully-qualified method
call to access the "private" method BAZ().  The second example will show
that it is impossible to override the BAZ() method.

	package FOO;

	sub new { bless {} }
	sub bar {
		my $self = shift;
		$self->FOO::private::BAZ;
	}

	package FOO::private;

	sub BAZ {
		print "in BAZ\n";
	}

	package main;

	$a = FOO->new;
	$a->bar;

Now we try to override the BAZ() method.  We would like FOO::bar() to call
GOOP::BAZ(), but this cannot happen because FOO::bar() explicitly calls
FOO::private::BAZ().

	package FOO;

	sub new { bless {} }
	sub bar {
		my $self = shift;
		$self->FOO::private::BAZ;
	}

	package FOO::private;

	sub BAZ {
		print "in BAZ\n";
	}

	package GOOP;
	@ISA = qw( FOO );
	sub new { bless {} }

	sub BAZ {
		print "in GOOP::BAZ\n";
	}

	package main;

	$a = GOOP->new;
	$a->bar;

To create reusable code we must modify class FOO, flattening class
FOO::private.  The next example shows a reusable class FOO which allows the
method GOOP::BAZ() to be used in place of FOO::BAZ().

	package FOO;

	sub new { bless {} }
	sub bar {
		my $self = shift;
		$self->BAZ;
	}

	sub BAZ {
		print "in BAZ\n";
	}

	package GOOP;
	@ISA = qw( FOO );

	sub new { bless {} }
	sub BAZ {
		print "in GOOP::BAZ\n";
	}

	package main;

	$a = GOOP->new;
	$a->bar;

=head1 CLASS CONTEXT AND THE OBJECT

Use the object to solve package and class context problems.  Everything a
method needs should be available via the object or should be passed as a
parameter to the method.

A class will sometimes have static or global data to be used by the
methods.  A subclass may want to override that data and replace it with new
data.  When this happens the superclass may not know how to find the new
copy of the data.

This problem can be solved by using the object to define the context of the
method.  Let the method look in the object for a reference to the data.  The
alternative is to force the method to go hunting for the data ("Is it in my
class, or in a subclass?  Which subclass?"), and this can be inconvenient
and will lead to hackery.  It is better to just let the object tell the
method where that data is located.

	package Bar;

	%fizzle = ( 'Password' => 'XYZZY' );

	sub new {
		my $self = {};
		$self->{'fizzle'} = \%fizzle;
		bless $self;
	}

	sub enter {
		my $self = shift;
	
		# Don't try to guess if we should use %Bar::fizzle
		# or %Foo::fizzle.  The object already knows which
		# we should use, so just ask it.
		#
		my $fizzle = $self->{'fizzle'};

		print "The word is ", $fizzle->{'Password'}, "\n";
	}

	package Foo;
	@ISA = qw( Bar );

	%fizzle = ( 'Password' => 'Rumple' );

	sub new {
		my $self = Bar->new;
		$self->{'fizzle'} = \%fizzle;
		bless $self;
	}

	package main;

	$a = Bar->new;
	$b = Foo->new;
	$a->enter;
	$b->enter;

=head1 INHERITING A CONSTRUCTOR

An inheritable constructor should use the second form of bless() which allows
blessing directly into a specified class.  Notice in this example that the
object will be a BAR not a FOO, even though the constructor is in class FOO.

	package FOO;

	sub new {
		my $type = shift;
		my $self = {};
		bless $self, $type;
	}

	sub baz {
		print "in FOO::baz()\n";
	}

	package BAR;
	@ISA = qw(FOO);

	sub baz {
		print "in BAR::baz()\n";
	}

	package main;

	$a = BAR->new;
	$a->baz;

=head1 DELEGATION

Some classes, such as SDBM_File, cannot be effectively subclassed because
they create foreign objects.  Such a class can be extended with some sort of
aggregation technique such as the "using" relationship mentioned earlier or
by delegation.

The following example demonstrates delegation using an AUTOLOAD() function to
perform message-forwarding.  This will allow the Mydbm object to behave
exactly like an SDBM_File object.  The Mydbm class could now extend the
behavior by adding custom FETCH() and STORE() methods, if this is desired.

	package Mydbm;

	require SDBM_File;
	require TieHash;
	@ISA = qw(TieHash);

	sub TIEHASH {
		my $type = shift;
		my $ref = SDBM_File->new(@_);
		bless {'delegate' => $ref};
	}

	sub AUTOLOAD {
		my $self = shift;

		# The Perl interpreter places the name of the
		# message in a variable called $AUTOLOAD.

		# DESTROY messages should never be propagated.
		return if $AUTOLOAD =~ /::DESTROY$/;

		# Remove the package name.
		$AUTOLOAD =~ s/^Mydbm:://;

		# Pass the message to the delegate.
		$self->{'delegate'}->$AUTOLOAD(@_);
	}

	package main;
	use Fcntl qw( O_RDWR O_CREAT );

	tie %foo, Mydbm, "adbm", O_RDWR|O_CREAT, 0640;
	$foo{'bar'} = 123;
	print "foo-bar = $foo{'bar'}\n";