IT박스

내 Perl 스크립트에 다른 파일의 함수를 어떻게 포함합니까?

itboxs 2020. 11. 14. 10:01
반응형

내 Perl 스크립트에 다른 파일의 함수를 어떻게 포함합니까?


이것은 정말 간단한 질문처럼 보이지만 어떻게 든 내 Google-Fu가 실패했습니다.

Perl에서 다른 파일의 함수를 포함하는 구문은 무엇입니까? C 같은 걸 찾고 있어요#include "blah.h"

Perl 모듈을 사용하는 옵션을 보았지만 현재 코드를 중요하게 다시 작성해야하는 것 같습니다.


모듈을 사용하십시오. perldoc perlmodExporter를 확인하십시오 .

Foo.pm 파일

package Foo;
use strict;
use warnings;
use Exporter;

our @ISA= qw( Exporter );

# these CAN be exported.
our @EXPORT_OK = qw( export_me export_me_too );

# these are exported by default.
our @EXPORT = qw( export_me );

sub export_me {
    # stuff
}

sub export_me_too {
    # stuff
}

1;

메인 프로그램에서 :

use strict;
use warnings;

use Foo;  # import default list of items.

export_me( 1 );

또는 두 기능을 모두 얻으려면 :

use strict;
use warnings;

use Foo qw( export_me export_me_too );  # import listed items

export_me( 1 );
export_me_too( 1 );

패키지 변수를 가져올 수도 있지만 권장되지 않습니다.


이 필요한 일을 할 것입니다. '필수'파일이 다음을 추가하여 진실을 반환하는지 확인해야합니다.

1;

파일 끝에.

다음은 작은 샘플입니다.

$ cat m1.pl 
use strict;
sub x { warn "aard"; }
1;

$ cat m2.pl 
use strict;
require "m1.pl";
x();

$ perl m2.pl 
aard at m1.pl line 2.

But migrate to modules as soon as you can.

EDIT

A few benefits of migrating code from scripts to modules:

  • Without packages, everything occupies a single namespace, so you may hit a situation where two functions from separate files want the same name.
  • A package allows you to expose some functions, but hide others. With no packages, all functions are visible.
  • Files included with require are only loaded at run time, whereas packages loaded with use are subject to earlier compile-time checks.

I believe you are looking for the require or use keywords.


Also, do 'file.pl'; will work, but modules are the better solution.


I know the question specifically says "functions", but I get this post high up in search when I look for "perl include", and often times (like now) I want to include variables (in a simple way, without having to think about modules). And so I hope it's OK to post my example here (see also: Perl require and variables; in brief: use require, and make sure both "includer" and "includee" files declare the variable as our):

$ perl --version

This is perl, v5.10.1 (*) built for i686-linux-gnu-thread-multi ...

$ cat inc.pl
use warnings;
use strict;

our $xxx = "Testing";

1;

$ cat testA.pl 
use warnings;
use strict;

require "inc.pl";
our $xxx;

print "1-$xxx-\n";
print "Done\n";

$ perl testA.pl 
1-Testing-
Done


$ cat testB.pl 
use warnings;
use strict;

our $xxx;
print "1-$xxx-\n";

$xxx="Z";
print "2-$xxx-\n";

require "inc.pl";

print "3-$xxx-\n";
print "Done\n";

$ perl testB.pl 
Use of uninitialized value $xxx in concatenation (.) or string at testB.pl line 5.
1--
2-Z-
3-Testing-
Done

You really should look into perl modules however, for a quick hack you could always run "perl -P" which runs your perl script through the C pre-processor. That means you can do #include and friends....

Only a quick hack though, beware ;-)


What are you looking for is 'require file.pl', but what you should be looking at is 'use module'.


The above answers all ignored the client part: How to import the module.

See the accepted answer here: How do I use a Perl module from a relative location?

Without the trick in this answer, you'll have plenty of trouble trying to get the module path right when you use $mymodule;

참고URL : https://stackoverflow.com/questions/1712016/how-do-i-include-functions-from-another-file-in-my-perl-script

반응형