2014-06-18 3 views
3

저는 perl에 익숙하지 않습니다. (일반적으로 프로그래밍이지만 Python에 익숙합니다.)perl에서 초기화되지 않은 값 오류가 발생했습니다.

use strict; 
use warnings; 
use diagnostics; 

my $simple_variable = "string"; 
print my $simple_variable; 

기본적으로 나는이 스크립트는 초기화되지 않은 값 오류를 반환 왜 변수가 명확하게 정의되어 있기 때문에, 알고 싶어요.

감사

+2

범위 지정에 대해 배웠습니다 !! 더 많은 것을 배우기위한 고전적인 온라인 참고 문헌은 [Coping with Scoping] (http://perl.plover.com/FAQs/Namespaces.html)이고 [PerlMonks 노드] (http://www.perlmonks.org/? node_id = 66677)를 Perl의 가변 스코핑에 적용합니다. –

+0

당신은'print "를 원한다. $ simple_variable \ n";''print'가 자동으로 그 일을하지 않기 때문에'\ n'을 끝에 두어야 만한다. –

답변

3

my 변수를 만들고 미확정 (스칼라) 또는 빈 (배열과 해시)에 초기화합니다. 또한 생성 한 변수를 반환합니다. 이와 같이

,

print my $simple_variable; 

이미이 때문에 펄을 요구하는 이유 당신은 잘 모르겠어요

my $simple_variable = "string"; 
print $simple_variable; 

할 의미

my $simple_variable = undef; 
print $simple_variable; 

같은 일이 너에게 많이 말했다. 프로그램에서 다음을 출력합니다.

"my" variable $simple_variable masks earlier declaration in same scope at a.pl 
     line 6 (#1) 
    (W misc) A "my", "our" or "state" variable has been redeclared in the 
    current scope or statement, effectively eliminating all access to the 
    previous instance. This is almost always a typographical error. Note 
    that the earlier variable will still exist until the end of the scope 
    or until all closure referents to it are destroyed. 

"새로운 인스턴스가 이전 인스턴스에 대한 모든 액세스를 효과적으로 제거"하는 효과에 유의하십시오.

관련 문제