2009-02-08 9 views
5

나는 Perl에서 여러 개의 모듈로 응용 프로그램을 작성 중이다.Perl에서 정수를 여러 모듈로 가져올 수 있습니까?

#Constants.pm 
$h0 = 0; 
$scale = 20; 

을 그리고 여러 모듈에서 main:: 또는 Constants::과 자격없이 사용할 :이처럼 사방에서 볼 수 있습니다 약간의 전역 상수를 작성합니다. 그러나 하나 이상의 모듈에 use Constants;을 쓰면 하나의 네임 스페이스에만 가져올 수 있습니다. 이 주위에 어떤 방법이 있습니까?

최신 ActivePerl을 사용하고 있습니다. 이 경우

package main; 

당신이 main 네임 스페이스에있을 것입니다 정의 모든 변수 :

$main::x 

또는 당신은 용감 느낌이 있다면을

답변

2

당신은 Constants.pm의 상단에이를 넣을 수 있습니다 :

package; 

이 경우 모든 정의 변수는 병이 빈 공간에있을 : 네임 스페이스로 package을 사용하는 것은 권장하지 않습니다, 그리고 분명히 펄의 일부 버전에서 사용되지됩니다

$::x 

참고. 아래 인용문을보십시오.


man perlfunc에서 인용 :

 

     package NAMESPACE 
     package Declares the compilation unit as being in the given 
       namespace. The scope of the package declaration is 
       from the declaration itself through the end of the 
       enclosing block, file, or eval (the same as the "my" 
       operator). All further unqualified dynamic identifiers 
       will be in this namespace. A package statement affects 
       only dynamic variables--including those you've used 
       "local" on--but not lexical variables, which are cre? 
       ated with "my". Typically it would be the first decla? 
       ration in a file to be included by the "require" or 
       "use" operator. You can switch into a package in more 
       than one place; it merely influences which symbol table 
       is used by the compiler for the rest of that block. 
       You can refer to variables and filehandles in other 
       packages by prefixing the identifier with the package 
       name and a double colon: $Package::Variable. If the 
       package name is null, the "main" package as assumed. 
       That is, $::sail is equivalent to $main::sail (as well 
       as to $main'sail, still seen in older code). 

       If NAMESPACE is omitted, then there is no current pack? 
       age, and all identifiers must be fully qualified or 
       lexicals. However, you are strongly advised not to 
       make use of this feature. Its use can cause unexpected 
       behaviour, even crashing some versions of Perl. It is 
       deprecated, and will be removed from a future release. 


편집 :이 질문뿐만 아니라 도움이 될 수 있습니다 How do I use constants from a Perl module?

+0

감사합니다. 내가 정말로 필요로하는 것은 "main"을 생략하는 것이다. – Lev

+0

실제로 작동하는지 궁금합니다. 그렇다면 답의 맨 위에 면책 조항을 제거하겠습니다. –

+1

이것은 상수를 main ::에 넣었으나 이제 모든 상수 앞에 main :: –

7

체크 아웃 Exporterperlmod 매뉴얼 페이지를 참조하십시오.

5

아무에게도 말하지 말아라. Perl의 특수 변수는 이다. $global 실제로 $Foo::global라고 때문이다

{ package Foo; 
our $global = 42; } 

{ package Bar; 
say "global is $global"; } 

: 당신은 아마이 일을하지 않는 것으로 나타났습니다. 당신은 도 아마이 "규칙"이 변수는 항상 가 main에 있다고 생각하기 때문이다 그 등 @INC, %ENV, $_, 같은 것들을 적용되지 않는 것으로 나타났습니다.

하지만 실제로는 그 변수 그 이상입니다. 전체 글로브 은 main에 "강제"됩니다.

{ package Constants; 
    $_{PI} = 3.141592; } 

{ package Foo; 
    say "pi is $_{PI}"; } 

하고 작동합니다 : 그것은 당신이 다음처럼 작성할 수 있다는 것을 의미합니다.

혹시 실제 코드에서이 작업을 수행하는 경우, 그러나, 누군가가 당신에게 을 살해 할 것으로 예상

(동일 $ENV, &INC 등 적용) :하지만 알고 좋은 단지의 경우 당신은 누군가 다른 사람을 볼 그것을하고 있습니다.

2

당신은이 같은 Exporter를 사용할 수 있습니다

Constants.pm에서

:

 
#Constants.pm 
require Exporter; 
@ISA = qw(Exporter); 
@EXPORT = qw($h0 $scale); 
@EXPORT_OK = qw(myfunc); 

$h0 = 0; 
$scale = 20; 
sub myfunc {...} 

참고 :
* @EXPORT 배열의 &myfunc&는 선택 사항이며, 그것은 당신이하지 권장 그걸 써. *이 $h0 기본적으로 $scale, 그리고 명시 적으로 요청 유일의 경우 &이 myfunc을 보냅니다

을 (클라이언트 모듈에 의해 수입되는 기호를 지정하는 방법은 아래 참조) 그리고 Constants.pm을 가져오고 싶은 모듈 $h0, $scale 또는 &myfunc을 사용하려면 다음을 추가하여 모두의 기호가 @EXPORT 인 Constants.pm에 들여 오기하십시오.

 
#MyModule.pm 
use Constants qw(; 

당신은 단지 기호의 일부를 가져 오려면

사용 : 당신이 Constant.pm`s 기호 중 하나를 가져 오지 않는 경우

 
#MyModule.pm 
use Constants qw($h0); 

그리고 마지막으로, 사용

 
#MyModule.pm 
use Constants(); 
7

이 코드는 사용자가 원하는대로 정확하게 처리해야합니다. 모든 칭호를 lkundrak에게 보냅니다.

package Constants; 

use base qw/Exporter/; 

use constant BOB => 666; 
use constant ALICE => 555; 

sub import { 
    no strict "refs"; 

    ${[caller]->[0].'::'}{$_} = ${__PACKAGE__."::"}{$_} 
     foreach grep { not /^(ISA|isa|BEGIN|import|Dumper)$/ } 
      keys %{__PACKAGE__."::"}; 
} 
+0

이것이 상수를 다루는 유일한 대답 인 것은 재미 있습니다. (심지어 질문조차도 실제로 그것들을 건드리지 않습니다). – Cornelius

+0

이 답변에 있어야합니다. 고마워! – Ishikawa91