annotate lib/IMPL/require.pm @ 418:3f38dabaf5cc ref20150831

sync
author cin
date Mon, 28 Dec 2015 15:11:35 +0300
parents c6e90e02dd17
children b0481c071bea
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 {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 my ( $self, $aliases ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 return unless $aliases;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 die "A hash reference is required" unless ref $aliases eq 'HASH';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 my $caller = caller;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 $PENDING{$caller} = 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 no strict 'refs';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 while ( my ( $alias, $class ) = each %$aliases ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 _trace("$alias => $class");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 $level++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 $class = _require($class);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 *{"${caller}::$alias"} = sub () {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 $class;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 $level--;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 delete $PENDING{$caller};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 sub _require {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43 my ($class) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 if ( not $class =~ s/^-// ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 ( my $file = $class ) =~ s/::|'/\//g;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 _trace("already pending") and return $class
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 if $PENDING{$class};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 $PENDING{$class} = 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 _trace("loading $file.pm");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51 $level++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 require "$file.pm";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 $level--;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 _trace("loaded $file.pm");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 delete $PENDING{$class};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 $class;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 sub _trace {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 my ($message) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 $LOADER_LOG->print( "\t" x $level, "$message\n" ) if $LOADER_LOG;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 return 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 C<IMPL::require> загружает и назначет псевдонимы модулям.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 use IMPL::require {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 TFoo => 'My::Nested::Package::Foo',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 FS => 'File::Spec'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 my $obj = My::Nested::Package::Foo->new('foo');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 $obj = TFoo->new('foo'); # ditto
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 FS->catdir('one','two','three');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 Загружает модули с помощью C<require> и создает константы которые возвращаю полное имя модуля.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 =cut