'''펄 모듈'''(Perl module)은 언어의 소프트웨어 구성 요소이다. 모듈은 마치 자바 패키지처럼 소스 코드가 ''CGI'', ''Net:FTP'', 같은 이름공간을 정의하기 위한 매커니즘인 패키지 안에 있을 있도록 정의한다. 즉, 파일 구조는 이름공간 구조를 미러링한다. (예: Net::FTP를 위한 소스 코드는 Net/FTP.pm에 속한다) == 예제 == "Hello, World!"의 예는 여러 스타일로 구현할 있다. 펄에서는 모듈이 반드시 필요한 것이 아니다. 함수와 코드는 어디에서든 정의하여 사용할 있다. 아래는 단지 예를 들기 위한 부분이다. 클래스가 필요한 자바와 다르다. "Hello, World"의 함수는 다음과 같이 작성할 있다: sub hello "Hello, world!\n" print hello(); 아니면 단순히 줄로도 가능하다: print "Hello, world!\n"; === 절차적 예제 === 절차적 모듈로 구현한 "Hello, World"의 예는 다음과 같다. 모듈 이용을 기술하는 짧은 스크립트도 포함되어 있다. ---- #!/usr/bin/perl Loads the module and imports any functions into our namespace (defaults to "main") exported by the module. Hello::World exports hello() by default. Exports can usually be controlled by the caller. use Hello::World; print hello(); prints "Hello, world!\n" print hello("Milky Way"); prints "Hello, Milky Way!\n" ---- ---- "package" is the namespace where the module's resides. It dictates the name of the file if you want it to be "use"d. If more than one word, it constrains the location of the module. package Hello::World; By default Perl allows you to use variables without declaring them. This may be convenient for short scripts and one-liners. But in longer unit of code such as module it is wise to declare your variables both to catch typos and to constrain their accessibility appropriately from outside the module. The strict pragma forces you to declare your variables. use strict; Similarly, Perl does not issue most compiler or run-time warnings by default. More complicated scripts, such as most modules, will usually find them very helpful for debugging. The warnings pragma turns on optional warnings. use warnings; module's version number is stored in certain forms of the "use" built-in depend on this variable being defined. our $VERSION '1.00'; Inherit from the "Exporter" module which handles exporting functions. Most procedural modules make use of this. use base 'Exporter'; When the module is invoked, export, by default, the function "hello" into the namespace of the using code. our @EXPORT qw(hello); Lines starting with an equal sign indicate embedded POD documentation. POD sections end with an =cut directive, and can be intermixed almost freely with normal code. =head1 NAME Hello::World An encapsulation of common output message =head1 SYNOPSIS use Hello::World; print hello(); print hello("Milky Way"); =head1 DESCRIPTION This is procedural module which gives you the famous "Hello, world!" message, and it’s even customizable! =head2 Functions The following functions are exported by default =head3 hello print hello(); print hello($target); Returns the famous greeting. If is given it will be used, otherwise "world" is the target of your greeting. =cut define the function hello(). sub hello my $target shift; $target 'world' unless defined $target; return "Hello, $target!\n"; =head1 AUTHOR Joe Hacker =cut Perl module must end with true value or else it is considered not to have loaded. By convention this value is usually though it can be any true value. module can end with false to indicate failure but this is rarely used and it would instead die() (exit with an error). 1; ---- === 객체 지향 예제 === 동일하지만 객제 지향 스타일로 예는 다음과 같다. 객체 지향 모듈의 이점은 객체가 다른 객체와 독립적으로 구성될 있다는 점이다. ---- #!/usr/bin/perl use Hello::World; my $hello $hello->print; prints "Hello, world!\n" Way"); $hello->print; prints "Hello, Milky Way!\n" my $greeting => "Pittsburgh"); prints "Hello, Pittsburgh!\n" $hello->print; still prints "Hello, Milky Way!\n" ---- ---- In Perl there is no special 'class' definition. namespace is class. package Hello::World; use strict; use warnings; our $VERSION "1.00"; =head1 NAME Hello::World An encapsulation of common output message =head1 SYNOPSIS use Hello::World; my $hello $hello->print; =head1 DESCRIPTION This is an object-oriented library which can print the famous "H.W." message. =head2 Methods =head3 new my $hello my $hello target => $target ); Instantiates an object which holds greeting message. If is given it is passed to Ctarget >>. =cut The constructor of an object is called new() by convention. Any method may construct an object and you can have as many as you like. sub new my($class, %args) @_; my $self bless({}, $class); my $target exists $args{target} $args{target} "world"; $self->{target} $target; return $self; =head3 target my $target $hello->target; Gets and sets the current target of our message. =cut sub target my $self shift; if( @_ my $target shift; $self->{target} $target; return =head3 to_string my $greeting Returns the $greeting as string =cut sub to_string my $self shift; return "Hello, =head3 print $hello->print; Outputs the greeting to STDOUT =cut sub print my $self shift; print "\n"; =head1 AUTHOR Joe Hacker =cut 1; ---- == 같이 보기 == CPAN == 외부 링크 == Perl modules (packages and symbol tables) 분류:펄