line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WeakRef; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
239644
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
48
|
|
4
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
146
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
require Exporter; |
7
|
|
|
|
|
|
|
require DynaLoader; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
@ISA = qw(Exporter DynaLoader); |
10
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
11
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
12
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
13
|
|
|
|
|
|
|
@EXPORT = qw( |
14
|
|
|
|
|
|
|
weaken isweak |
15
|
|
|
|
|
|
|
); |
16
|
|
|
|
|
|
|
$VERSION = '0.01'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
bootstrap WeakRef $VERSION; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Preloaded methods go here. |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
1; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 NAME |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
WeakRef -- an API to the Perl weak references |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 SYNOPSIS |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
use WeakRef; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
for($i=0; $i<100000; $i++) { |
33
|
|
|
|
|
|
|
my $x = {}; |
34
|
|
|
|
|
|
|
my $y = {}; |
35
|
|
|
|
|
|
|
$x->{Y} = $y; |
36
|
|
|
|
|
|
|
$y->{X} = $y; |
37
|
|
|
|
|
|
|
weaken($x->{Y}); |
38
|
|
|
|
|
|
|
} # no memory leak |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
if(isweak($ref)) { |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 DESCRIPTION |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
A patch to Perl 5.005_55 by the author implements a core API for weak |
46
|
|
|
|
|
|
|
references. This module is a Perl-level interface to that API, allowing |
47
|
|
|
|
|
|
|
weak references to be created in Perl. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
A weak reference is just like an ordinary Perl reference except that |
50
|
|
|
|
|
|
|
it isn't included in the reference count of the thing referred to. |
51
|
|
|
|
|
|
|
This means that once all references to a particular piece of data are |
52
|
|
|
|
|
|
|
weak, the piece of data is freed and all the weak references are set |
53
|
|
|
|
|
|
|
to undef. This is particularly useful for implementing circular |
54
|
|
|
|
|
|
|
data structures without memory leaks or caches of objects. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
The command |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
use WeakRef; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
exports two symbols to the user's namespace by default: C and |
61
|
|
|
|
|
|
|
C. C takes a single argument, the reference to be weakened, |
62
|
|
|
|
|
|
|
and returns the same value. |
63
|
|
|
|
|
|
|
The idiom |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
weaken($this->{Thing}->{Parent} = $this); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
is useful. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
The C command takes a single parameter and returns true if |
70
|
|
|
|
|
|
|
the parameter is a weak reference, undef otherwise. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 BUGS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
None known. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 AUTHOR |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Tuomas J. Lukka lukka@iki.fi |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Copyright (c) 1999 Tuomas J. Lukka. All rights reserved. This |
81
|
|
|
|
|
|
|
program is free software; you can redistribute it and/or modify it under the |
82
|
|
|
|
|
|
|
same terms as perl itself. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 BLATANT PLUG |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
This module and the patch to the core Perl were written in connection |
87
|
|
|
|
|
|
|
with the APress book `Tuomas J. Lukka's Definitive Guide to Object-Oriented |
88
|
|
|
|
|
|
|
Programming in Perl', to avoid explaining why certain things would have |
89
|
|
|
|
|
|
|
to be done in cumbersome ways. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=cut |