File Coverage

/root/.lib_archive_extract/1.07/Callback.pm
Criterion Covered Total %
statement 3 53 5.6
branch 0 20 0.0
condition 0 3 0.0
subroutine 1 6 16.6
pod 0 4 0.0
total 4 86 4.6


line stmt bran cond sub pod time code
1              
2             package Callback;
3              
4             require Exporter;
5             require UNIVERSAL;
6              
7             $VERSION = $VERSION = 1.07;
8             @ISA = (Exporter);
9             @EXPORT_OK = qw(@callbackTrace);
10              
11 1     1   7 use strict;
  1         2  
  1         463  
12              
13             sub new
14             {
15 0     0 0   my ($package,$func,@args) = @_;
16 0           my ($p, $file, $line) = caller(0);
17 0           my @method;
18 0 0 0       if (ref $func ne 'CODE' && UNIVERSAL::isa($func, "UNIVERSAL")) {
19 0 0         if ($func->isa('Callback')) {
20 0 0         return $func unless @args;
21 0           my $new = bless { %$func }, $package;
22 0           push(@{$new->{ARGS}}, @args);
  0            
23 0           return $new;
24             } else {
25 0           my $method = shift @args;
26 0           my $obj = $func;
27 0           $func = $obj->can($method);
28 0 0         unless (defined $func) {
29 0           require Carp;
30 0           Carp::croak("Can't locate method '$method' for object $obj");
31             }
32 0           unshift(@args, $obj);
33 0           @method = (METHOD => $method); # For Storable hooks
34             }
35             }
36 0           my $x = {
37             FUNC => $func,
38             ARGS => [@args],
39             CALLER => "$file:$line",
40             @method
41             };
42 0           return bless $x, $package;
43             }
44              
45             sub call
46             {
47 0     0 0   my ($this, @args) = @_;
48 0           my ($ret, @ret);
49              
50 0           unshift(@Callback::callbackTrace, $this->{CALLER});
51 0 0         if (wantarray) {
52 0           @ret = eval {&{$this->{FUNC}}(@{$this->{ARGS}},@args)};
  0            
  0            
  0            
53             } else {
54 0           $ret = eval {&{$this->{FUNC}}(@{$this->{ARGS}},@args)};
  0            
  0            
  0            
55             }
56 0           shift(@Callback::callbackTrace);
57 0 0         die $@ if $@;
58 0 0         return @ret if wantarray;
59 0           return $ret;
60             }
61              
62             sub DELETE
63       0     {
64             }
65              
66             #
67             # Storable hooks
68             #
69             # We cannot serialize something containing a pure CODE ref, which is the
70             # case if there's no METHOD attribute in the object.
71             #
72             # However, when Callback is a method call, we can remove the FUNC attribute
73             # and serialize the object: the function address will be recomputed at
74             # retrieve time.
75             #
76              
77             sub STORABLE_freeze {
78 0     0 0   my ($self, $cloning) = @_;
79 0 0         return if $cloning;
80              
81 0           my %copy = %$self;
82             die "cannot store $self since it contains CODE references\n"
83 0 0         unless exists $copy{METHOD};
84              
85 0           delete $copy{FUNC};
86 0           return ("", \%copy);
87             }
88              
89             sub STORABLE_thaw {
90 0     0 0   my ($self, $cloning, $x, $copy) = @_;
91              
92 0           %$self = %$copy;
93              
94 0           my $method = $self->{METHOD};
95 0           my $obj = $self->{ARGS}->[0];
96 0           my $func = $obj->can($method);
97 0 0         die("cannot restore $self: can't locate method '$method' on object $obj")
98             unless defined $func;
99              
100 0           $self->{FUNC} = $func;
101 0           return;
102             }
103              
104             1;
105