| 1 | package Attribute::Tie; | ||||||||||||||||||
| 2 | # | ||||||||||||||||||
| 3 | # $Id: Tie.pm,v 0.2 2009/02/08 09:00:12 dankogai Exp $ | ||||||||||||||||||
| 4 | # | ||||||||||||||||||
| 5 | 5 | ▶ | use 5.008001; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 5 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 5 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 5 | ▶ | use Attribute::Handlers; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 5 | our $VERSION = sprintf "%d.%02d", q$Revision: 0.2 $ =~ /(\d+)/g; | |||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | 5 | our %MOD2TIE; | |||||||||||||||||
| 12 | 5 | our %SIGIL = qw/HASH % ARRAY @ SCALAR $/; | |||||||||||||||||
| 13 | 5 | our $ERROR = \&error; | |||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | 3 | ▶ | sub seterror { | ||||||||||||||||
|
Subroutine
seterror: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||
| 16 | 3 | my $pkg = shift; | |||||||||||||||||
| 17 | $ERROR = | ||||||||||||||||||
| 18 | ref $_[0] eq 'CODE' ? $_[0] | ||||||||||||||||||
| 19 | : $_[0] ? \&error | ||||||||||||||||||
| 20 | 1 | ▶ | : sub { } | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 21 | 3 | ▶ | } | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | 12 | ▶ | sub load { | ||||||||||||||||
|
Subroutine
load: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||
| 24 | 12 | my ($mod2tie, $ref) = @_; | |||||||||||||||||
| 25 | 12 | ▶ | return $MOD2TIE{$mod2tie} if $MOD2TIE{$mod2tie}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 26 | { # Maybe you don't need to load at all | ||||||||||||||||||
| 27 | 5 | ▶ | no strict 'refs'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 28 | 9 | my $type = ref $ref; | |||||||||||||||||
| 29 | return $MOD2TIE{$mod2tie} = $mod2tie | ||||||||||||||||||
| 30 | 9 | ▶ | if defined &{$mod2tie . "::TIE$type" }; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 31 | return $MOD2TIE{$mod2tie} = 'Tie::'.$mod2tie | ||||||||||||||||||
| 32 | 9 | ▶ | if defined &{'Tie::'. $mod2tie . "::TIE$type"}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 33 | } | ||||||||||||||||||
| 34 | # DB_File, et al. | ||||||||||||||||||
| 35 | 6 | eval qq{ require $mod2tie }; | |||||||||||||||||
| 36 | 6 | ▶ | return $MOD2TIE{$mod2tie} = $mod2tie unless $@; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | # Anything else | ||||||||||||||||||
| 39 | 4 | eval qq{ require Tie::$mod2tie }; | |||||||||||||||||
| 40 | 4 | ▶ | return $MOD2TIE{$mod2tie} = 'Tie::'.$mod2tie unless $@; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | # Report Failure and die | ||||||||||||||||||
| 43 | 3 | my ( $pkg, $file, $line ) = caller(4); | |||||||||||||||||
| 44 | 3 | die "Neither $mod2tie nor Tie::$mod2tie is available", | |||||||||||||||||
| 45 | " at $file line $line\n"; | ||||||||||||||||||
| 46 | } | ||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | 4 | ▶ | sub error { | ||||||||||||||||
|
Subroutine
error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 49 | 4 | my ( $ref, $mod2tie, @tiearg ) = @_; | |||||||||||||||||
| 50 | 4 | my ( $pkg, $file, $line ) = caller(4); | |||||||||||||||||
| 51 | 4 | my $s = $SIGIL{ ref $ref }; | |||||||||||||||||
| 52 | 4 | die "tie(", join( ", ", $s . ref $ref, qq('$mod2tie'), @tiearg ), | |||||||||||||||||
| 53 | ") failed : $! at $file line $line\n"; | ||||||||||||||||||
| 54 | } | ||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | 12 | ▶ | sub UNIVERSAL::Tie : ATTR { | ||||||||||||||||
|
Subroutine
Tie: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||
| 57 | 12 | my ( $pkg, $sym, $ref, $attr, $data, $phase ) = @_; | |||||||||||||||||
| 58 | 12 | ▶ | my @tiearg = ref $data ? @$data : ($data); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 59 | 12 | my $mod2tie = Attribute::Tie::load(shift @tiearg, $ref); | |||||||||||||||||
| 60 | 9 | ▶ | my $obj = | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 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 | ▶ | $Attribute::Tie::ERROR->( $ref, $mod2tie, @tiearg ) if !$obj; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 66 | 5 | ▶ | } | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | 5 | 1; | |||||||||||||||||
| 69 | __END__ | ||||||||||||||||||