annotate Lib/IMPL/require.pm @ 252:34a3f8668b58

fixed IMPL::require IMPL::declare
author sergey
date Mon, 19 Nov 2012 01:28:58 +0400
parents 9f394b27dccf
children 0f59b2de72af
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
1 package IMPL::require;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
2 use Scalar::Util qw(set_prototype);
205
891c04080658 IMPL::Web::View fixed template selection, release candidate
sergey
parents: 197
diff changeset
3 use strict;
251
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
4 #require IMPL::Code::Loader;
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
5
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
6 our %PENDING;
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
7 our $LOADER_LOG;
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
8
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
9 our $level = 0;
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
10
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
11 sub import {
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
12 my ($self, $aliases) = @_;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
13
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
14 return unless $aliases;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
15
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
16 die "A hash reference is required" unless ref $aliases eq 'HASH';
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
17
205
891c04080658 IMPL::Web::View fixed template selection, release candidate
sergey
parents: 197
diff changeset
18 my $caller = caller;
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
19
252
34a3f8668b58 fixed IMPL::require IMPL::declare
sergey
parents: 251
diff changeset
20 $PENDING{$caller} = 1;
34a3f8668b58 fixed IMPL::require IMPL::declare
sergey
parents: 251
diff changeset
21
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
22 no strict 'refs';
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
23
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
24 while( my ($alias, $class) = each %$aliases ) {
251
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
25 _trace("$alias => $class");
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
26 $level++;
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
27
238
b8c724f6de36 DOM model refactoring
sergey
parents: 230
diff changeset
28 $class = _require($class);
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
29
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
30 *{"${caller}::$alias"} = set_prototype(sub {
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
31 $class
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
32 }, '');
251
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
33
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
34 $level--;
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
35 }
252
34a3f8668b58 fixed IMPL::require IMPL::declare
sergey
parents: 251
diff changeset
36
34a3f8668b58 fixed IMPL::require IMPL::declare
sergey
parents: 251
diff changeset
37 delete $PENDING{$caller};
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
38 }
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
39
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 205
diff changeset
40 sub _require {
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 205
diff changeset
41 my ($class) = @_;
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 205
diff changeset
42
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 205
diff changeset
43 if ( not $class =~ s/^-// ) {
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 205
diff changeset
44 ( my $file = $class ) =~ s/::|'/\//g;
252
34a3f8668b58 fixed IMPL::require IMPL::declare
sergey
parents: 251
diff changeset
45 _trace("already pending") and return $class
251
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
46 if $PENDING{$class};
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
47 $PENDING{$class} = 1;
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
48 _trace("loading $file.pm");
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
49 $level++;
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 205
diff changeset
50 require "$file.pm";
251
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
51 $level--;
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
52 _trace("loaded $file.pm");
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
53 delete $PENDING{$class};
230
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 205
diff changeset
54 }
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 205
diff changeset
55 $class;
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 205
diff changeset
56 }
6d8092d8ce1b *reworked IMPL::Security
sergey
parents: 205
diff changeset
57
251
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
58 sub _trace {
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
59 my ($message) = @_;
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
60
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
61 $LOADER_LOG->print("\t" x $level ,"$message\n") if $LOADER_LOG;
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
62
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
63 return 1;
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
64 }
9f394b27dccf require can handle recursive module references
sergey
parents: 238
diff changeset
65
197
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
66 1;
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
67
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
68 __END__
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
69
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
70 =pod
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
71
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
72 =head1 NAME
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
73
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
74 C<IMPL::require> загружает и назначет псевдонимы модулям.
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
75
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
76 =head1 SYNOPSIS
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
77
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
78 =begin code
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
79
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
80 use IMPL::require {
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
81 TFoo => 'My::Nested::Package::Foo',
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
82 FS => 'File::Spec'
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
83 };
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
84
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
85 my $obj = My::Nested::Package::Foo->new('foo');
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
86 $obj = TFoo->new('foo'); # ditto
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
87
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
88 FS->catdir('one','two','three');
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
89
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
90 =end code
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
91
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
92 =head1 DESCRIPTION
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
93
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
94 Загружает модули с помощью C<require> и создает константы которые возвращаю полное имя модуля.
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
95
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
96
6b1dda998839 Added IMPL::declare, IMPL::require, to simplify module definitions
sergey
parents:
diff changeset
97 =cut