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