407
|
1 package IMPL::require;
|
|
2 use strict;
|
|
3
|
|
4 #require IMPL::Code::Loader;
|
|
5
|
|
6 use Carp qw(carp);
|
|
7
|
|
8 our %PENDING;
|
|
9 our $LOADER_LOG;
|
|
10
|
|
11 our $level = 0;
|
|
12
|
|
13 sub import {
|
425
|
14 my $self = shift;
|
|
15
|
|
16 my $aliases;
|
407
|
17
|
425
|
18 if ( @_ == 1 ) {
|
|
19 my $aliases = shift;
|
|
20 die "A hash reference is required" unless ref $aliases eq 'HASH';
|
|
21 }
|
|
22 else {
|
|
23 die "A list of pairs is expected" unless @_ % 2 == 0;
|
|
24 $aliases = {@_};
|
|
25 }
|
407
|
26
|
|
27 my $caller = caller;
|
|
28
|
|
29 $PENDING{$caller} = 1;
|
|
30
|
|
31 no strict 'refs';
|
425
|
32 while ( my ( $alias, $spec ) = each %$aliases ) {
|
|
33 my ( $mode, $class ) = m/^(-|~)(.*)/;
|
407
|
34
|
425
|
35 _trace("$alias => $spec [$class]");
|
|
36
|
|
37 $class =~ s/^SELF(?=\W|$)/${caller}::/;
|
407
|
38 $level++;
|
|
39
|
425
|
40 if ( $mode eq '-' ) {
|
|
41 *{"${caller}::$alias"} = sub () {
|
|
42 $class;
|
|
43 };
|
|
44 }
|
|
45 elsif ( $mode eq '~' ) {
|
|
46 *{"${caller}::$alias"} = sub () {
|
|
47 my $c = _require($class);
|
|
48 *{"${caller}::$alias"} = sub() { $c };
|
|
49 return $c;
|
|
50 };
|
|
51 }
|
|
52 else {
|
|
53 my $c = _require($class);
|
|
54 *{"${caller}::$alias"} = sub () {
|
|
55 $c;
|
|
56 };
|
|
57 }
|
407
|
58
|
|
59 $level--;
|
|
60 }
|
|
61
|
|
62 delete $PENDING{$caller};
|
|
63 }
|
|
64
|
|
65 sub _require {
|
|
66 my ($class) = @_;
|
|
67
|
|
68 if ( not $class =~ s/^-// ) {
|
|
69 ( my $file = $class ) =~ s/::|'/\//g;
|
|
70 _trace("already pending") and return $class
|
|
71 if $PENDING{$class};
|
|
72 $PENDING{$class} = 1;
|
|
73 _trace("loading $file.pm");
|
|
74 $level++;
|
|
75 require "$file.pm";
|
|
76 $level--;
|
|
77 _trace("loaded $file.pm");
|
|
78 delete $PENDING{$class};
|
|
79 }
|
|
80 $class;
|
|
81 }
|
|
82
|
|
83 sub _trace {
|
|
84 my ($message) = @_;
|
|
85
|
|
86 $LOADER_LOG->print( "\t" x $level, "$message\n" ) if $LOADER_LOG;
|
|
87
|
|
88 return 1;
|
|
89 }
|
|
90
|
|
91 1;
|
|
92
|
|
93 __END__
|
|
94
|
|
95 =pod
|
|
96
|
|
97 =head1 NAME
|
|
98
|
|
99 C<IMPL::require> загружает и назначет псевдонимы модулям.
|
|
100
|
|
101 =head1 SYNOPSIS
|
|
102
|
|
103 =begin code
|
|
104
|
|
105 use IMPL::require {
|
|
106 TFoo => 'My::Nested::Package::Foo',
|
|
107 FS => 'File::Spec'
|
|
108 };
|
|
109
|
|
110 my $obj = My::Nested::Package::Foo->new('foo');
|
|
111 $obj = TFoo->new('foo'); # ditto
|
|
112
|
|
113 FS->catdir('one','two','three');
|
|
114
|
|
115 =end code
|
|
116
|
|
117 =head1 DESCRIPTION
|
|
118
|
|
119 Загружает модули с помощью C<require> и создает константы которые возвращаю полное имя модуля.
|
|
120
|
|
121
|
|
122 =cut
|