line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Object::Util::Stringify; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
66600
|
use strict; |
|
1
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
30
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
5
|
use Scalar::Util qw(blessed refaddr); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
62
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
7
|
use Exporter qw(import); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
326
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
11
|
|
|
|
|
|
|
our $DATE = '2021-09-28'; # DATE |
12
|
|
|
|
|
|
|
our $DIST = 'Object-Util-Stringify'; # DIST |
13
|
|
|
|
|
|
|
our $VERSION = '0.001'; # VERSION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
16
|
|
|
|
|
|
|
set_stringify |
17
|
|
|
|
|
|
|
unset_stringify |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my %Overloaded_Packages; |
21
|
|
|
|
|
|
|
my %Object_Strings; # key=refaddr, val=string |
22
|
|
|
|
|
|
|
sub set_stringify { |
23
|
3
|
|
|
3
|
1
|
1436
|
require overload; |
24
|
|
|
|
|
|
|
|
25
|
3
|
|
|
|
|
9
|
my ($obj, $str) = @_; |
26
|
|
|
|
|
|
|
|
27
|
3
|
50
|
|
|
|
16
|
die "First argument must be a blessed reference" unless blessed($obj); |
28
|
|
|
|
|
|
|
|
29
|
3
|
|
|
|
|
7
|
my $obj_pkg = ref $obj; |
30
|
3
|
|
|
|
|
11
|
$Object_Strings{ refaddr($obj) } = $str; |
31
|
|
|
|
|
|
|
$obj_pkg->overload::OVERLOAD(q("") => \&_overload_string) |
32
|
3
|
100
|
|
|
|
18
|
unless $Overloaded_Packages{$obj_pkg}++; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# return the obj for convenience |
35
|
3
|
|
|
|
|
53
|
$obj; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub unset_stringify { |
39
|
2
|
|
|
2
|
1
|
10
|
require overload; |
40
|
|
|
|
|
|
|
|
41
|
2
|
|
|
|
|
5
|
my ($obj, $str) = @_; |
42
|
|
|
|
|
|
|
|
43
|
2
|
50
|
|
|
|
10
|
die "First argument must be a blessed reference" unless blessed($obj); |
44
|
|
|
|
|
|
|
|
45
|
2
|
|
|
|
|
15
|
my $obj_pkg = ref $obj; |
46
|
2
|
|
|
|
|
11
|
delete $Object_Strings{ refaddr($obj) }; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# return the obj for convenience |
49
|
2
|
|
|
|
|
5
|
$obj; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub _overload_string { |
53
|
5
|
|
|
5
|
|
40
|
my $obj = shift; |
54
|
5
|
|
|
|
|
15
|
my $key = refaddr $obj; |
55
|
5
|
100
|
|
|
|
30
|
exists($Object_Strings{$key}) ? $Object_Strings{$key} : $obj; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
1; |
59
|
|
|
|
|
|
|
# ABSTRACT: Utility routines related to object stringification |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
__END__ |