2010-06-04 34 views

回答

0

只是要清楚,你在看隨機Perl代碼隨機包?

或者對於Perl 模塊,例如, 「a/b/c/d1.pm」模塊「a :: b :: c :: d1」?

在任何一種情況下,都不能使用單個「use」語句來加載它們。

你需要做的是找到所有適當的文件,使用globFile::Find

在第一種情況下(模塊),則可以或者通過require -ing每個文件,或者通過轉換成文件名的模塊名稱(s#/#::#g; s#\.pm$##;)和調用每個模塊上use單獨加載它們。

至於嵌套在隨機的Perl文件實際的包,這些包可以是:

  • 由grepping每個文件中列出的(再次,通過globFile::Find),選擇/^package (.*);/

  • 實際加載通過爲每個文件執行require $file

    在這種情況下,請注意,a/b/c/1.pl中的每個軟件包的包名稱將爲NOT需要與「a :: b :: c」相關 - 例如,它們可以由文件作者「p1」,「a :: p1」或「a :: b :: c :: p1_something」命名。

1

通常,諸如a/b/c.pl之類的腳本不會有除main以外的命名空間。也許您正在考慮發現模塊,其名稱如a/b/c.pm(這是一個錯誤的名稱,因爲較低的包名通常是爲Perl內部保留的)。

然而,由於目錄路徑,你可以看看使用File::Find潛在 Perl模塊:

use strict; 
use warnings; 
use File::Find; 
use Data::Dumper; 

my @modules; 
sub wanted 
{ 
    push @modules, $_ if m/\.pm$/ 
} 
find(\&wanted, 'A/B'); 

print "possible modules found:\n"; 
print Dumper(\@modules)' 
7

如果要加載的所有模塊在包括具有一定的前綴(如a::b::c下的一切路徑,你可以使用Module::Find

例如:

use Module::Find 'useall'; 

my @loaded = useall 'Foo::Bar::Baz'; # loads everything under Foo::Bar::Baz 

這取決於您@INC路徑正在建立必要的目錄,所以做任何需要的操作(例如,用use lib)第一。

1

這可能是矯枉過正,但你之前和加載模塊後,檢查符號表,看看有什麼變化:

use strict; use warnings; 
my %original = map { $_ => 1 } get_namespaces("::"); 
require Inline; 
print "New namespaces since 'require Inline' call are:\n"; 
my @new_namespaces = sort grep !defined $original{$_}, get_namespaces("::"); 
foreach my $new_namespace (@new_namespaces) { 
    print "\t$new_namespace\n"; 
} 

sub get_namespaces { 
    # recursively inspect symbol table for known namespaces 
    my $pkg = shift; 
    my @namespace =(); 
    my %s = eval "%" . $pkg; 
    foreach my $key (grep /::$/, keys %s) { 
    next if $key eq "main::"; 
    push @namespace, "$pkg$key", get_namespaces("$pkg$key"); 
    } 
    return @namespace; 
} 

 
New namespaces since 'require Inline' call are: 
     ::AutoLoader:: 
     ::Config:: 
     ::Digest:: 
     ::Digest::MD5:: 
     ::Dos:: 
     ::EPOC:: 
     ::Exporter:: 
     ::Exporter::Heavy:: 
     ::File:: 
     ::File::Spec:: 
     ::File::Spec::Cygwin:: 
     ::File::Spec::Unix:: 
     ::File::Spec::Win32:: 
     ::Inline::Files:: 
     ::Inline::denter:: 
     ::Scalar:: 
     ::Scalar::Util:: 
     ::Socket:: 
     ::VMS:: 
     ::VMS::Filespec:: 
     ::XSLoader:: 
     ::vars:: 
     ::warnings::register:: 
相關問題