lib/AI/MicroStructure/Alias.pm untested

filter: Statementstmt 0.0% 0 / 40 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 49 CC 2 SCAR 11.5
CC
2
Cov
0%
init
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 3.1
1 package AI::MicroStructure::Alias;
2 use strict;
3 use warnings;
4 use Carp;
5  
6 our $VERSION = '1.000';
7  
8 sub init {
9 my ( $self, $alias ) = @_;
10 my $class = caller(0);
11  
12 eval "require AI::MicroStructure::$alias;";
13 croak "Aliased structure AI::MicroStructure::$alias failed to load: $@"
14 if $@;
15  
16 no strict 'refs';
17 no warnings;
18  
19 # copy almost everything over from the original
20 for my $k ( grep { ! /^(?:Theme|meta|import)$/ }
21 keys %{"AI::MicroStructure::$alias\::"} )
22 {
23 *{"$class\::$k"} = *{"AI::MicroStructure::$alias\::$k"};
24 }
25  
26 # local things
27 ${"$class\::Theme"} = ( split /::/, $class )[-1];
28 ${"$class\::meta"} = $class->new();
29 *{"$class\::import"} = sub {
30 my $callpkg = caller(0);
31 my $structure = ${"$class\::Theme"};
32 my $meta = $class->new();
33 *{"$callpkg\::meta$structure"} = sub { $meta->name(@_) };
34 };
35 }
36  
37 1;
38  
39 __END__
40  
41 =head1 NAME
42  
43 AI::MicroStructure::Alias - Alias one structure to another
44  
45 =head1 SYNOPSIS
46  
47 package AI::MicroStructure::bonk;
48 use AI::MicroStructure::Alias;
49 our @ISA = qw( AI::MicroStructure::Alias );
50 __PACKAGE__->init('batman');
51 1;
52  
53 =head1 NAME
54
55 AI::MicroStructure::bonk - The bonk structure
56
57 =head1 DESCRIPTION
58
59 This structure is just an alias of the C<batman> theme.
60  
61 =cut
62
63 # no __DATA__ section required!
64  
65 =head1 DESCRIPTION
66  
67 C<AI::MicroStructure::Alias> is the base class for any structures that is
68 simply an alias of another structure.
69  
70 =head1 METHOD
71  
72 AI::MicroStructure::Alias defines a single method, C<init()> that
73 make aliases very easy to write (see the full example in L<SYNOPSIS>):
74  
75 =over 4
76  
77 =item init( $original )
78  
79 C<init()> must be called when the subclass is loaded, so as to correctly
80 load and alias the original structure.
81  
82 C<$original> is the name of the original structure we want to alias.
83  
84 =back
85  
86 =head1 AUTHOR
87  
88 Philippe 'BooK' Bruhat, C<< <book@cpan.org> >>
89  
90 =head1 COPYRIGHT & LICENSE
91  
92 Copyright 2006 Philippe 'BooK' Bruhat, All Rights Reserved.
93  
94 This program is free software; you can redistribute it and/or modify it
95 under the same terms as Perl itself.
96  
97 =cut
98