2013-05-16 35 views
-3

我想从我的程序中调用一个函数。因此,我可以将函数保存在另一个文件中,而不是与我调用该函数的脚本混合。如果是这样,我应该使用什么名称用函数保存文件。请帮助..perl编程使函数调用

+0

文件名根本无关紧要。 – m0skit0

回答

1

您可以编写一个模块。这里是一个小模块:

文件Foo.pm

# declare a namespace 
package Foo; 

# always use these, they help you find errors 
use strict; use warnings; 

sub foo { 
    print "Hello, this is Foo\n"; 
} 

# a true value as last statement. 
1; 

那个文件放到相同的目录中的脚本:

文件script.pl

use strict; use warnings; 

# load the module 
use Foo; 

Foo::foo(); # invoke the function via the fully qualified name 

为了创建更好的模块,您可能会考虑面向对象或Exporter模块。