annotate lib/IMPL/require.pm @ 427:09e0086a82a7 ref20150831 tip

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