File Coverage

lib/Object/Disoriented.pm
Criterion Covered Total %
statement 27 29 93.1
branch 3 6 50.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 37 43 86.0


line stmt bran cond sub pod time code
1             package Object::Disoriented;
2              
3 1     1   770 use strict;
  1         3  
  1         50  
4 1     1   7 use warnings;
  1         2  
  1         62  
5              
6             our $VERSION = '0.02';
7              
8 1     1   17 use Carp qw;
  1         3  
  1         436  
9              
10             sub import {
11 2     2   1197 my (undef, $package, @functions) = @_;
12              
13 2 50       8 croak "What functions in $package do you want to disorient?"
14             if !@functions;
15              
16 2 50       91 if (!eval "CORE::require $package; 1") {
17             # Make the error message look like the caller's.
18 0         0 $@ =~ s/\n* \s+ at \s+ \(eval \s+ \d+\) \s+ line \s+ \d+\.\n*\z//xms;
19 0         0 croak $@;
20             }
21              
22             # Create a (presumably spurious) instance
23 2         14 my $instance = $package->new;
24              
25             # Ensure all desired functions exist as methods
26 2         11 my @missing = grep { !$instance->can($_) } @functions;
  4         32  
27 2 50       6 croak "Methods not found in $package: @missing"
28             if @missing;
29              
30             # Build a sub for each desired function
31 2         4 my $caller = caller;
32 2         5 for my $name (@functions) {
33 4     4   18 set_symbol($caller, $name, sub { $instance->$name(@_) });
  4         1355  
34             }
35              
36 2         22 return;
37             }
38              
39             sub set_symbol {
40 4     4 0 7 my ($package, $name, $value) = @_;
41 1     1   7 no strict qw;
  1         2  
  1         78  
42 4         5 *{"$package\::$name"} = $value;
  4         21  
43             }
44              
45             1;
46             __END__