Created
December 10, 2011 03:03
-
-
Save aero/1454423 to your computer and use it in GitHub Desktop.
Understanding use namespace::autoclean;
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env perl | |
| use 5.010; | |
| my $aref = []; | |
| { | |
| package My; | |
| use List::Util qw/max/; | |
| use namespace::autoclean; # 렉시컬 영역이 끝나면 이 이전에 import된 함수는 패키지에서 제거 | |
| use Scalar::Util qw/reftype/; | |
| say '# My'; | |
| say reftype $aref; # ARRAY 출력 | |
| say max(2,3,1); # 3 출력 | |
| } # My 패키지의 렉시컬 영역종료 별도 .pm파일로 만들어도 파일끝날때 렉시컬 영역이 종료 | |
| # 한파일 안에 있으므로 use My; 안해도 됨 | |
| say '# main'; | |
| say My::reftype $aref; # ARRAY 출력. 여전히 사용 가능 | |
| #say My::max(4,1,2); # namespace::autoclean 에 의해 제거됐으므로 사용 불가능 | |
| { | |
| package MyObject; | |
| use Moose; | |
| #use namespace::autoclean; # 제대로 하려면 이거 주석풀고 넣어야 됨 | |
| has a => ( is => 'rw', default => 7 ); | |
| __PACKAGE__->meta->make_immutable; | |
| } | |
| # 한파일 안에 있으므로 use MyObject; 안해도 됨 | |
| my $o = MyObject->new(); | |
| say $o->a; # 7 출력 | |
| # 아래줄은 "has" is defined 출력 has는 Moose객체 내부에서만 필요하다. | |
| # MyObject 객체에서 use namespace::autoclean; 주석을 풀면 has는 제거된다. | |
| # 따라서 use Moose; use Moose::Role;등 Moose류 모듈을 로딩한 다음에 | |
| # use namespace::autoclean; 을 넣어주는 이유는 | |
| # Moose로드시 import되는 has, with, extends 등등 같은 syntax sugar들이 | |
| # 모듈 로딩후 제거되어 Moose객체를 사용하는 코드들에서 접근불가능 하게 하고 | |
| # 제거 하지 않은 경우 혹시나 sugar함수와 같은 함수 이름을 사용자가 실수로 | |
| # 정의해서 사용할 경우 뒤죽박죽되지 않게 하기 위함이다. | |
| # 추가참고: http://aero.sarang.net/blog/2010/03/no-moose-or-use-namespaceclean.html | |
| say '"has" is defined' if defined &MyObject::has; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment