| 1 | package aliased::factory; | ||||||||||
| 2 | 2 | $VERSION = v0.0.1; | |||||||||
| 3 | |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | =head1 NAME | ||||||||||
| 9 | |||||||||||
| 10 | aliased::factory - shorter versions of a class tree's constructors | ||||||||||
| 11 | |||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||
| 13 | |||||||||||
| 14 | use aliased::factory YAPI => 'Yahoo::Marketing'; | ||||||||||
| 15 | |||||||||||
| 16 | my $service = YAPI->KeywordResearchService->new(...); | ||||||||||
| 17 | |||||||||||
| 18 | my $res = $service->getRelatedKeywords( | ||||||||||
| 19 | relatedKeywordRequest => | ||||||||||
| 20 | YAPI->RelatedKeywordRequestType->new(...) | ||||||||||
| 21 | ); | ||||||||||
| 22 | |||||||||||
| 23 | =head1 About | ||||||||||
| 24 | |||||||||||
| 25 | This package is similar to L<aliased>, but performs on-demand loading | ||||||||||
| 26 | for packages below the shortened 'root package'. For example, the above | ||||||||||
| 27 | code will automatically load the KeywordResearchService and | ||||||||||
| 28 | RelatedKeywordRequestType packages from the Yahoo::Marketing:: | ||||||||||
| 29 | hierarchy. | ||||||||||
| 30 | |||||||||||
| 31 | To load a second-level package: | ||||||||||
| 32 | |||||||||||
| 33 | use aliased::factory BAR => 'Foo::Bar'; | ||||||||||
| 34 | |||||||||||
| 35 | my $bort = BAR->Baz->Bort->new(...); | ||||||||||
| 36 | |||||||||||
| 37 | This would load the Foo::Bar::Baz and then Foo::Bar::Baz::Bort packages. | ||||||||||
| 38 | Each method call require()s the corresponding package and returns an | ||||||||||
| 39 | aliased::factory object, which has a new() method (see below.) | ||||||||||
| 40 | |||||||||||
| 41 | =cut | ||||||||||
| 42 | |||||||||||
| 43 | 12 | ▶ | my $new_factory = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 12 | my $class = shift; | |||||||||
| 45 | 12 | bless \(shift) => $class; | |||||||||
| 46 | 2 | }; | |||||||||
| 47 | |||||||||||
| 48 | 2 | my $err; | |||||||||
| 49 | |||||||||||
| 50 | 16 | ▶ | my $load = sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 51 | 16 | my $package = shift; | |||||||||
| 52 | 16 | $package =~ s#::#/#g; | |||||||||
| 53 | 16 | $package .= '.pm'; | |||||||||
| 54 | 16 | ▶ | return 1 if(exists $INC{$package}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | |||||||||||
| 56 | 11 | local $@; | |||||||||
| 57 | 11 | my $ans = eval {require($package)}; | |||||||||
| 58 | 11 | ▶ | if($err = $@) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 4 | my $f = __FILE__; | |||||||||
| 60 | 4 | ($err = $@) =~ s/ at $f line \d+\.\n//; | |||||||||
| 61 | 4 | return; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 7 | return($ans); | |||||||||
| 65 | 2 | }; | |||||||||
| 66 | |||||||||||
| 67 | =head1 Factory Method | ||||||||||
| 68 | |||||||||||
| 69 | =head2 new | ||||||||||
| 70 | |||||||||||
| 71 | Returns a new object of the class represented by the $factory object. | ||||||||||
| 72 | |||||||||||
| 73 | my $instantiated = $factory->new(...); | ||||||||||
| 74 | |||||||||||
| 75 | The class being instantiated must have a new() method. | ||||||||||
| 76 | |||||||||||
| 77 | =cut | ||||||||||
| 78 | |||||||||||
| 79 | 7 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 80 | 7 | my $self = shift; | |||||||||
| 81 | 7 | return $$self->new(@_); | |||||||||
| 82 | } ###################################################################### | ||||||||||
| 83 | |||||||||||
| 84 | =head1 Meta Methods | ||||||||||
| 85 | |||||||||||
| 86 | The rest of this is functionality used to create the factory. | ||||||||||
| 87 | |||||||||||
| 88 | =head2 import | ||||||||||
| 89 | |||||||||||
| 90 | Installs a sub 'shortname' in your package containing an object pointed | ||||||||||
| 91 | at $package. | ||||||||||
| 92 | |||||||||||
| 93 | aliased::factory->import(shortname => $package); | ||||||||||
| 94 | |||||||||||
| 95 | =cut | ||||||||||
| 96 | |||||||||||
| 97 | 2 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=5 SCAR=16.5
| |||||||||||
| 98 | 2 | my $class = shift; | |||||||||
| 99 | 2 | ▶ | @_ or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | |||||||||||
| 101 | 1 | my ($alias, $package, @also) = @_; | |||||||||
| 102 | 1 | ▶ | croak("error") if(@also); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | |||||||||||
| 104 | 1 | my $caller = caller; | |||||||||
| 105 | |||||||||||
| 106 | 1 | ▶ | unless(defined $package) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 0 | $package = $alias; | |||||||||
| 108 | 0 | $alias =~ s/.*:://; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 1 | ▶ | $load->($package) or croak($err); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | |||||||||||
| 113 | 1 | my $obj = $class->$new_factory($package); | |||||||||
| 114 | 2 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 115 | 1 | ▶ | *{$caller . '::' . $alias} = sub { $obj }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 116 | } ###################################################################### | ||||||||||
| 117 | |||||||||||
| 118 | =head2 can | ||||||||||
| 119 | |||||||||||
| 120 | When called on a factory object, attempts to require the subpackage. If | ||||||||||
| 121 | this succeeds, it will return a coderef (which will return the | ||||||||||
| 122 | subfactory when executed.) | ||||||||||
| 123 | |||||||||||
| 124 | my $coderef = $factory->can('subpackage'); | ||||||||||
| 125 | |||||||||||
| 126 | This method is used by AUTOLOAD(). | ||||||||||
| 127 | |||||||||||
| 128 | =cut | ||||||||||
| 129 | |||||||||||
| 130 | 15 | ▶ | sub can { | ||||||||
|
Subroutine
can: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 131 | 15 | my $self = shift; | |||||||||
| 132 | 15 | ▶ | my $class = ref($self) or return($self->SUPER::can(@_)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 15 | my ($subpack) = @_; | |||||||||
| 134 | |||||||||||
| 135 | 15 | my $package = $$self . '::' . $subpack; | |||||||||
| 136 | 15 | ▶ | $load->($package) or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | 11 | my $obj = $class->$new_factory($package); | |||||||||
| 138 | 11 | ▶ | return sub {$obj}; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 139 | } ###################################################################### | ||||||||||
| 140 | |||||||||||
| 141 | =head2 AUTOLOAD | ||||||||||
| 142 | |||||||||||
| 143 | Attempts to load the $factory's corresponding subpackage, returns a the | ||||||||||
| 144 | subfactory object or throws a fatal error: "Can't locate ... in @INC | ||||||||||
| 145 | ...". | ||||||||||
| 146 | |||||||||||
| 147 | my $subfactory = $factory->subpackage; | ||||||||||
| 148 | |||||||||||
| 149 | =cut | ||||||||||
| 150 | |||||||||||
| 151 | 22 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: called CC=4 SCAR=13.9
| |||||||||||
| 152 | 22 | my $self = shift; | |||||||||
| 153 | 22 | ▶ | @_ and croak("subfactories cannot have arguments"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 154 | |||||||||||
| 155 | 22 | (my $method = our $AUTOLOAD) =~ s/.*:://; | |||||||||
| 156 | 22 | ▶ | return if $method eq 'DESTROY'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | 11 | ▶ | my $sub = $self->can($method) or croak($err); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 158 | 10 | return $sub->(); | |||||||||
| 159 | } ###################################################################### | ||||||||||
| 160 | |||||||||||
| 161 | =head1 AUTHOR | ||||||||||
| 162 | |||||||||||
| 163 | Eric Wilhelm @ <ewilhelm at cpan dot org> | ||||||||||
| 164 | |||||||||||
| 165 | http://scratchcomputing.com/ | ||||||||||
| 166 | |||||||||||
| 167 | =head1 BUGS | ||||||||||
| 168 | |||||||||||
| 169 | If you found this module on CPAN, please report any bugs or feature | ||||||||||
| 170 | requests through the web interface at L<http://rt.cpan.org>. I will be | ||||||||||
| 171 | notified, and then you'll automatically be notified of progress on your | ||||||||||
| 172 | bug as I make changes. | ||||||||||
| 173 | |||||||||||
| 174 | If you pulled this development version from my /svn/, please contact me | ||||||||||
| 175 | directly. | ||||||||||
| 176 | |||||||||||
| 177 | =head1 COPYRIGHT | ||||||||||
| 178 | |||||||||||
| 179 | Copyright (C) 2009 Eric L. Wilhelm, All Rights Reserved. | ||||||||||
| 180 | |||||||||||
| 181 | =head1 Acknowledgements | ||||||||||
| 182 | |||||||||||
| 183 | Thanks to HDP for the suggestion of using a factory object. | ||||||||||
| 184 | |||||||||||
| 185 | =head1 NO WARRANTY | ||||||||||
| 186 | |||||||||||
| 187 | Absolutely, positively NO WARRANTY, neither express or implied, is | ||||||||||
| 188 | offered with this software. You use this software at your own risk. In | ||||||||||
| 189 | case of loss, no person or entity owes you anything whatsoever. You | ||||||||||
| 190 | have been warned. | ||||||||||
| 191 | |||||||||||
| 192 | =head1 LICENSE | ||||||||||
| 193 | |||||||||||
| 194 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 195 | under the same terms as Perl itself. | ||||||||||
| 196 | |||||||||||
| 197 | =cut | ||||||||||
| 198 | |||||||||||
| 199 | # vi:ts=2:sw=2:et:sta | ||||||||||
| 200 | 2 | 1; | |||||||||