Added libalien-wxwidgets-perl
[pkg-perl] / deb-src / libalien-wxwidgets-perl / libalien-wxwidgets-perl-0.50 / inc / Module / Load.pm
1 package Module::Load;\r
2 \r
3 $VERSION = '0.10';\r
4 \r
5 use strict;\r
6 use File::Spec ();\r
7 \r
8 sub import {\r
9     my $who = _who();\r
10 \r
11     {   no strict 'refs';\r
12         *{"${who}::load"} = *load;\r
13     }\r
14 }\r
15 \r
16 sub load (*;@)  {\r
17     my $mod = shift or return;\r
18     my $who = _who();\r
19 \r
20     if( _is_file( $mod ) ) {\r
21         require $mod;\r
22     } else {\r
23         LOAD: {\r
24             my $err;\r
25             for my $flag ( qw[1 0] ) {\r
26                 my $file = _to_file( $mod, $flag);\r
27                 eval { require $file };\r
28                 $@ ? $err .= $@ : last LOAD;\r
29             }\r
30             die $err if $err;\r
31         }\r
32     }\r
33     __PACKAGE__->_export_to_level(1, $mod, @_) if @_;\r
34 }\r
35 \r
36 ### 5.004's Exporter doesn't have export_to_level.\r
37 ### Taken from Michael Schwerns Test::More and slightly modified\r
38 sub _export_to_level {\r
39     my $pkg     = shift;\r
40     my $level   = shift;\r
41     my $mod     = shift;\r
42     my $callpkg = caller($level);\r
43 \r
44     $mod->export($callpkg, @_);\r
45 }\r
46 \r
47 sub _to_file{\r
48     local $_    = shift;\r
49     my $pm      = shift || '';\r
50 \r
51     my @parts = split /::/;\r
52 \r
53     ### because of [perl #19213], see caveats ###\r
54     my $file = $^O eq 'MSWin32'\r
55                     ? join "/", @parts\r
56                     : File::Spec->catfile( @parts );\r
57 \r
58     $file   .= '.pm' if $pm;\r
59 \r
60     return $file;\r
61 }\r
62 \r
63 sub _who { (caller(1))[0] }\r
64 \r
65 sub _is_file {\r
66     local $_ = shift;\r
67     return  /^\./               ? 1 :\r
68             /[^\w:']/           ? 1 :\r
69             undef\r
70     #' silly bbedit..\r
71 }\r
72 \r
73 \r
74 1;\r
75 \r
76 __END__\r
77 \r
78 =pod\r
79 \r
80 =head1 NAME\r
81 \r
82 Module::Load - runtime require of both modules and files\r
83 \r
84 =head1 SYNOPSIS\r
85 \r
86         use Module::Load;\r
87 \r
88     my $module = 'Data:Dumper';\r
89     load Data::Dumper;      # loads that module\r
90     load 'Data::Dumper';    # ditto\r
91     load $module            # tritto\r
92     \r
93     my $script = 'some/script.pl'\r
94     load $script;\r
95     load 'some/script.pl';      # use quotes because of punctuations\r
96     \r
97     load thing;             # try 'thing' first, then 'thing.pm'\r
98 \r
99     load CGI, ':standard'   # like 'use CGI qw[:standard]'\r
100     \r
101 \r
102 =head1 DESCRIPTION\r
103 \r
104 C<load> eliminates the need to know whether you are trying to require\r
105 either a file or a module.\r
106 \r
107 If you consult C<perldoc -f require> you will see that C<require> will\r
108 behave differently when given a bareword or a string.\r
109 \r
110 In the case of a string, C<require> assumes you are wanting to load a\r
111 file. But in the case of a bareword, it assumes you mean a module.\r
112 \r
113 This gives nasty overhead when you are trying to dynamically require\r
114 modules at runtime, since you will need to change the module notation\r
115 (C<Acme::Comment>) to a file notation fitting the particular platform\r
116 you are on.\r
117 \r
118 C<load> elimates the need for this overhead and will just DWYM.\r
119 \r
120 =head1 Rules\r
121 \r
122 C<load> has the following rules to decide what it thinks you want:\r
123 \r
124 =over 4\r
125 \r
126 =item *\r
127 \r
128 If the argument has any characters in it other than those matching\r
129 C<\w>, C<:> or C<'>, it must be a file\r
130 \r
131 =item *\r
132 \r
133 If the argument matches only C<[\w:']>, it must be a module\r
134 \r
135 =item *\r
136 \r
137 If the argument matches only C<\w>, it could either be a module or a\r
138 file. We will try to find C<file> first in C<@INC> and if that fails,\r
139 we will try to find C<file.pm> in @INC.\r
140 If both fail, we die with the respective error messages.\r
141 \r
142 =back\r
143 \r
144 =head1 Caveats\r
145 \r
146 Because of a bug in perl (#19213), at least in version 5.6.1, we have\r
147 to hardcode the path seperator for a require on Win32 to be C</>, like\r
148 on Unix rather than the Win32 C<\>. Otherwise perl will not read it's\r
149 own %INC accurately double load files if they are required again, or\r
150 in the worst case, core dump.\r
151 \r
152 C<Module::Load> can not do implicit imports, only explicit imports.\r
153 (in other words, you always have to specify expliclity what you wish\r
154 to import from a module, even if the functions are in that modules'\r
155 C<@EXPORT>)\r
156 \r
157 =head1 AUTHOR\r
158 \r
159 This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.\r
160 \r
161 Thanks to Jonas B. Nielsen for making explicit imports work.\r
162 \r
163 =head1 COPYRIGHT\r
164 \r
165 This module is\r
166 copyright (c) 2002 Jos Boumans E<lt>kane@cpan.orgE<gt>.\r
167 All rights reserved.\r
168 \r
169 This library is free software;\r
170 you may redistribute and/or modify it under the same\r
171 terms as Perl itself.\r
172 \r
173 =cut                               \r