| 1 | # Copyright (c) 2011-16, Mitchell Cooper | ||
| 2 | # | ||
| 3 | # Evented::Object: a simple yet featureful base class event framework. | ||
| 4 | # https://github.com/cooper/evented-object | ||
| 5 | # | ||
| 6 | package Evented::Object::Hax; | ||
| 7 | |||
| 8 | use warnings; | ||
| 9 | use strict; | ||
| 10 | use 5.010; | ||
| 11 | |||
| 12 | use List::Util qw(first); | ||
| 13 | |||
| 14 | our $VERSION = '5.63'; | ||
| 15 | |||
| 16 | # exported import subroutine. | ||
| 17 | sub import { | ||
| 18 | my ($export_pkg, $import_pkg, @import) = (shift, (caller)[0], @_); | ||
| 19 | |||
| 20 | # import each item. | ||
| 21 | foreach my $item (@import) { | ||
| 22 | my $code = $export_pkg->can($item) or next; | ||
| 23 | export_code($import_pkg, $item, $code); | ||
| 24 | } | ||
| 25 | |||
| 26 | } | ||
| 27 | |||
| 28 | # fetch a symbol from package's symbol table. | ||
| 29 | sub get_symbol { | ||
| 30 | my ($package, $variable, $ref) = @_; | ||
| 31 | |||
| 32 | # must start with a sigil. | ||
| 33 | return if $variable !~ m/^([@\*%\$])(\w+)$/; | ||
| 34 | my ($sigil, $var_name) = ($1, $2); | ||
| 35 | |||
| 36 | my $symbol = $package.q(::).$var_name; | ||
| 37 | no strict 'refs'; | ||
| 38 | |||
| 39 | # find the symbol. | ||
| 40 | if ($sigil eq '$') { return $ref ? \$$symbol : $$symbol } | ||
| 41 | if ($sigil eq '@') { return $ref ? \@$symbol : @$symbol } | ||
| 42 | if ($sigil eq '*') { return $ref ? \*$symbol : *$symbol } | ||
| 43 | if ($sigil eq '%') { return $ref ? \&$symbol : %$symbol } | ||
| 44 | |||
| 45 | return; | ||
| 46 | } | ||
| 47 | |||
| 48 | # fetch a reference to a symbol. | ||
| 49 | sub get_symbol_ref { get_symbol(@_, 1) } | ||
| 50 | |||
| 51 | # set a symbol in package's symbol table. | ||
| 52 | sub set_symbol { | ||
| 53 | my ($package, $variable, @values) = @_; | ||
| 54 | |||
| 55 | # several symbols. | ||
| 56 | if (ref $variable && ref $variable eq 'HASH') { | ||
| 57 | set_symbol($package, $_, $variable->{$_}) foreach keys %$variable; | ||
| 58 | return; | ||
| 59 | } | ||
| 60 | |||
| 61 | # must start with a sigil. | ||
| 62 | return if $variable !~ m/^([@\*%\$])(\w+)$/; | ||
| 63 | my ($sigil, $var_name) = ($1, $2); | ||
| 64 | |||
| 65 | my $symbol = $package.q(::).$var_name; | ||
| 66 | no strict 'refs'; | ||
| 67 | |||
| 68 | # find the symbol. | ||
| 69 | if ($sigil eq '$') { $$symbol = $values[0] } | ||
| 70 | if ($sigil eq '@') { @$symbol = @values } | ||
| 71 | if ($sigil eq '*') { *$symbol = $values[0] } | ||
| 72 | if ($sigil eq '%') { %$symbol = @values } | ||
| 73 | |||
| 74 | return; | ||
| 75 | } | ||
| 76 | |||
| 77 | # export a subroutine. | ||
| 78 | # export_code('My::Package', 'my_sub', \&_my_sub) | ||
| 79 | sub export_code { | ||
| 80 | my ($package, $sub_name, $code) = @_; | ||
| 81 | no strict 'refs'; | ||
| 82 | *{"${package}::$sub_name"} = $code; | ||
| 83 | } | ||
| 84 | |||
| 85 | # delete a subroutine. | ||
| 86 | sub delete_code { | ||
| 87 | my ($package, $sub_name) = @_; | ||
| 88 | no strict 'refs'; | ||
| 89 | undef *{"${package}::$sub_name"}; | ||
| 90 | } | ||
| 91 | |||
| 92 | # adds a package to an ISA list if the | ||
| 93 | # package does not inherit from it already. | ||
| 94 | # package_make_child('Evented::Person', 'Evented::Object', 1) | ||
| 95 | sub make_child { | ||
| 96 | my ($package, $make_parent, $at_end) = @_; | ||
| 97 | my $isa = get_symbol_ref($package, '@ISA'); | ||
| 98 | |||
| 99 | # package already inherits directly. | ||
| 100 | return 1 if scalar first { $_ eq $make_parent } @$isa; | ||
| 101 | |||
| 102 | # check each class in ISA for inheritance. | ||
| 103 | foreach my $parent (@$isa) { | ||
| 104 | return 1 if $parent->isa($make_parent); | ||
| 105 | } | ||
| 106 | |||
| 107 | # add to ISA. | ||
| 108 | unshift @$isa, $make_parent unless $at_end; | ||
| 109 | push @$isa, $make_parent if $at_end; | ||
| 110 | |||
| 111 | return 1; | ||
| 112 | |||
| 113 | } | ||
| 114 | |||
| 115 | 1; |