line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Attribute::Tie; |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# $Id: Tie.pm,v 0.2 2009/02/08 09:00:12 dankogai Exp $ |
4
|
|
|
|
|
|
|
# |
5
|
5
|
|
|
5
|
|
65991
|
use 5.008001; |
|
5
|
|
|
|
|
16
|
|
|
5
|
|
|
|
|
427
|
|
6
|
5
|
|
|
5
|
|
27
|
use strict; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
720
|
|
7
|
5
|
|
|
5
|
|
26
|
use warnings; |
|
5
|
|
|
|
|
14
|
|
|
5
|
|
|
|
|
136
|
|
8
|
5
|
|
|
5
|
|
31140
|
use Attribute::Handlers; |
|
5
|
|
|
|
|
31300
|
|
|
5
|
|
|
|
|
33
|
|
9
|
|
|
|
|
|
|
our $VERSION = sprintf "%d.%02d", q$Revision: 0.2 $ =~ /(\d+)/g; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our %MOD2TIE; |
12
|
|
|
|
|
|
|
our %SIGIL = qw/HASH % ARRAY @ SCALAR $/; |
13
|
|
|
|
|
|
|
our $ERROR = \&error; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub seterror { |
16
|
3
|
|
|
3
|
0
|
2718
|
my $pkg = shift; |
17
|
|
|
|
|
|
|
$ERROR = |
18
|
|
|
|
|
|
|
ref $_[0] eq 'CODE' ? $_[0] |
19
|
|
|
|
|
|
|
: $_[0] ? \&error |
20
|
1
|
|
|
1
|
|
5
|
: sub { } |
21
|
3
|
100
|
|
|
|
26
|
} |
|
|
100
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub load { |
24
|
12
|
|
|
12
|
0
|
24
|
my ($mod2tie, $ref) = @_; |
25
|
12
|
100
|
|
|
|
60
|
return $MOD2TIE{$mod2tie} if $MOD2TIE{$mod2tie}; |
26
|
|
|
|
|
|
|
{ # Maybe you don't need to load at all |
27
|
5
|
|
|
5
|
|
1038
|
no strict 'refs'; |
|
5
|
|
|
|
|
13
|
|
|
5
|
|
|
|
|
2203
|
|
|
9
|
|
|
|
|
19
|
|
28
|
9
|
|
|
|
|
24
|
my $type = ref $ref; |
29
|
9
|
|
|
|
|
48
|
return $MOD2TIE{$mod2tie} = $mod2tie |
30
|
9
|
50
|
|
|
|
16
|
if defined &{$mod2tie . "::TIE$type" }; |
31
|
9
|
|
|
|
|
74
|
return $MOD2TIE{$mod2tie} = 'Tie::'.$mod2tie |
32
|
9
|
100
|
|
|
|
16
|
if defined &{'Tie::'. $mod2tie . "::TIE$type"}; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
# DB_File, et al. |
35
|
6
|
|
|
|
|
359
|
eval qq{ require $mod2tie }; |
36
|
6
|
100
|
|
|
|
3772
|
return $MOD2TIE{$mod2tie} = $mod2tie unless $@; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Anything else |
39
|
4
|
|
|
|
|
236
|
eval qq{ require Tie::$mod2tie }; |
40
|
4
|
100
|
|
|
|
25531
|
return $MOD2TIE{$mod2tie} = 'Tie::'.$mod2tie unless $@; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# Report Failure and die |
43
|
3
|
|
|
|
|
27
|
my ( $pkg, $file, $line ) = caller(4); |
44
|
3
|
|
|
|
|
66
|
die "Neither $mod2tie nor Tie::$mod2tie is available", |
45
|
|
|
|
|
|
|
" at $file line $line\n"; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub error { |
49
|
4
|
|
|
4
|
1
|
13
|
my ( $ref, $mod2tie, @tiearg ) = @_; |
50
|
4
|
|
|
|
|
45
|
my ( $pkg, $file, $line ) = caller(4); |
51
|
4
|
|
|
|
|
581
|
my $s = $SIGIL{ ref $ref }; |
52
|
4
|
|
|
|
|
131
|
die "tie(", join( ", ", $s . ref $ref, qq('$mod2tie'), @tiearg ), |
53
|
|
|
|
|
|
|
") failed : $! at $file line $line\n"; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub UNIVERSAL::Tie : ATTR { |
57
|
12
|
|
|
12
|
0
|
28677
|
my ( $pkg, $sym, $ref, $attr, $data, $phase ) = @_; |
58
|
12
|
50
|
|
|
|
69
|
my @tiearg = ref $data ? @$data : ($data); |
59
|
12
|
|
|
|
|
45
|
my $mod2tie = Attribute::Tie::load(shift @tiearg, $ref); |
60
|
9
|
50
|
|
|
|
239
|
my $obj = |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
61
|
|
|
|
|
|
|
ref $ref eq 'HASH' ? tie %$ref, $mod2tie, @tiearg |
62
|
|
|
|
|
|
|
: ref $ref eq 'ARRAY' ? tie @$ref, $mod2tie, @tiearg |
63
|
|
|
|
|
|
|
: ref $ref eq 'SCALAR' ? tie $$ref, $mod2tie, @tiearg |
64
|
|
|
|
|
|
|
: die "cannot tie to data type: ", ref $ref; |
65
|
9
|
100
|
|
|
|
246
|
$Attribute::Tie::ERROR->( $ref, $mod2tie, @tiearg ) if !$obj; |
66
|
5
|
|
|
5
|
|
36
|
} |
|
5
|
|
|
|
|
15
|
|
|
5
|
|
|
|
|
28
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
1; |
69
|
|
|
|
|
|
|
__END__ |