| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Variable::Disposition; |
|
2
|
|
|
|
|
|
|
# ABSTRACT: dispose of variables |
|
3
|
4
|
|
|
4
|
|
600701
|
use strict; |
|
|
4
|
|
|
|
|
6
|
|
|
|
4
|
|
|
|
|
138
|
|
|
4
|
4
|
|
|
4
|
|
16
|
use warnings; |
|
|
4
|
|
|
|
|
7
|
|
|
|
4
|
|
|
|
|
256
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
4
|
|
|
4
|
|
1433
|
use parent qw(Exporter); |
|
|
4
|
|
|
|
|
913
|
|
|
|
4
|
|
|
|
|
24
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.005'; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Variable::Disposition - helper functions for disposing of variables |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use feature qw(say); |
|
17
|
|
|
|
|
|
|
use Variable::Disposition; |
|
18
|
|
|
|
|
|
|
my $x = []; |
|
19
|
|
|
|
|
|
|
dispose $x; |
|
20
|
|
|
|
|
|
|
say '$x is no longer defined'; |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Provides some basic helper functions for making sure variables go away |
|
25
|
|
|
|
|
|
|
when you want them to. |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Currently provides L as a default import. To avoid this: |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
use Variable::Disposition (); |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
In addition, L and L are available as optional |
|
32
|
|
|
|
|
|
|
imports. |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
use Variable::Disposition qw(dispose retain retain_future); |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
The C< :all > tag can be used to import every available function: |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
use Variable::Disposition qw(:all); |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
but it would be safer to use a version instead: |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
use Variable::Disposition qw(:v1); |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
since these are guaranteed not to change in future. |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Other functions for use with L and L are likely to be |
|
47
|
|
|
|
|
|
|
added later. |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=cut |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
our @EXPORT_OK = qw(dispose retain retain_future); |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
|
54
|
|
|
|
|
|
|
all => [ @EXPORT_OK ], |
|
55
|
|
|
|
|
|
|
v1 => [ qw(dispose retain retain_future) ], |
|
56
|
|
|
|
|
|
|
); |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
our @EXPORT = qw(dispose); |
|
59
|
|
|
|
|
|
|
|
|
60
|
4
|
|
|
4
|
|
495
|
use Scalar::Util (); |
|
|
4
|
|
|
|
|
7
|
|
|
|
4
|
|
|
|
|
1026
|
|
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
our %RETAINED; |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 FUNCTIONS |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=cut |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head2 dispose |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Undefines the given variable, then checks that the original ref was destroyed. |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my $x = [1,2,3]; |
|
73
|
|
|
|
|
|
|
dispose $x; |
|
74
|
|
|
|
|
|
|
# $x is no longer defined. |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
This is primarily intended for cases where you no longer need a variable, and want |
|
77
|
|
|
|
|
|
|
to ensure that you haven't accidentally captured a strong reference to it elsewhere. |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Note that this clears the B's variable. |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
This function is defined with a prototype of ($), since it is only intended for use |
|
82
|
|
|
|
|
|
|
on scalar variables. To clear multiple variables, use a L loop: |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my ($x, $y, $z) = ...; |
|
85
|
|
|
|
|
|
|
dispose $_ for $x, $y, $z; |
|
86
|
|
|
|
|
|
|
is($x, undef); |
|
87
|
|
|
|
|
|
|
is($y, undef); |
|
88
|
|
|
|
|
|
|
is($z, undef); |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=cut |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub dispose($) { |
|
93
|
10
|
100
|
|
10
|
1
|
257596
|
die "Variable not defined" unless defined $_[0]; |
|
94
|
9
|
100
|
|
|
|
39
|
die "Variable was not a ref" unless ref $_[0]; |
|
95
|
8
|
|
|
|
|
34
|
delete $RETAINED{$_[0]}; # just in case we'd previously retained this one |
|
96
|
8
|
|
|
|
|
50
|
Scalar::Util::weaken(my $copy = $_[0]); |
|
97
|
8
|
|
|
|
|
62
|
undef $_[0]; |
|
98
|
8
|
100
|
|
|
|
45
|
die "Variable was not released" if defined $copy; |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head2 retain |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
Keeps a copy of this variable until program exit or L. |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Returns the original variable. |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=cut |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub retain($) { |
|
110
|
1
|
50
|
|
1
|
1
|
161666
|
die "Variable not defined" unless defined $_[0]; |
|
111
|
1
|
50
|
|
|
|
5
|
die "Variable was not a ref" unless ref $_[0]; |
|
112
|
1
|
|
|
|
|
4
|
$RETAINED{$_[0]} = $_[0]; |
|
113
|
1
|
|
|
|
|
8
|
$_[0] |
|
114
|
|
|
|
|
|
|
} |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 retain_future |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Holds a copy of the given L until it's marked ready, then releases our copy. |
|
119
|
|
|
|
|
|
|
Does not use L, since that could interfere with other callbacks attached |
|
120
|
|
|
|
|
|
|
to the L. |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Since Future 0.36, this behaviour is directly available via the L method, |
|
123
|
|
|
|
|
|
|
so it is recommended to use that instead of this function. |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Returns the original L. |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=cut |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub retain_future { |
|
130
|
6
|
|
|
6
|
1
|
197144
|
my ($f) = @_; |
|
131
|
6
|
50
|
|
|
|
73
|
die "Variable does not seem to be a Future, since it has no ->on_ready method" unless $f->can('on_ready'); |
|
132
|
6
|
|
|
|
|
25
|
return $f->retain; |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
1; |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
__END__ |