-3

プログラムから関数を呼び出したいのですが、関数を呼び出している場所からスクリプトと混合するのではなく、関数を別のファイルに保存できますか。 .

4

1 に答える 1

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

于 2013-05-16T10:43:44.887 に答える