본문 바로가기

Script/Perl

14. Class

 
펄은 원래 객체 지향 언어가 아니였다.
 
펄의 객체 지향 기능은 중간에 이식된 것이며 아래와 같은 특징이 있다.
 
- 객체는 단순한 참조이다. 다만 어떤 클래스에 속하는지
  알 수 있다는 점이 일반 참조와 구별되는 점이다.
 
- bless 를 사용하면 참조하고 있는 객체가 어떤 클래스에 속할지 정할 수 있다.
 
- ref 를 사용하면 참조하고 있는 것이 어떤 클래스인지 알아낼 수 있다. 
 
- 객체의 메서드는 $obj->method(); 식으로 호출한다.
 
- 클래스 메서드는 Package::Name->method() 식이다.
 
- 클래스는 메서드를 포함하게 된 패키지이다.
 
 
 
Constructor and destrutor
 
- 생성자의 첫번째 인자는 객체가 아니고 클래스 이름이다.
 
- 생성자의 두번째 인자는 객체 그 자체가 된다.
 
- 반드시 객체 자신을 리턴해야 하며 해당 객체가 어떤 객체인지 bless 를 통해 알려줘야 한다.
 
- 따라서 항상 아래의 구조가 된다.
 
use strict;
use warnings;
 
package Person;
sub new
{
    my $class = shift;
    my $self = {
        _firstName => shift ,
        _lastName  => shift ,
        _ssn       => shift ,
    };
    # Print all the values just for clarification.
    print "First Name is $self->{_firstName}\n" ;
    print "Last Name is $self->{_lastName}\n";
    print "SSN is $self->{_ssn}\n";
    bless $self , $ class;
    return $self ;
}
 
sub DESTROY
{
    print "   MyClass::DESTROY called\n";
}
 
my $object = new Person( "Sam", "Kim" , 233222);
 
간단히 살펴보면 이렇다.
 
package Person;
sub new
{
    my $class = shift;
    my $self = { 여기에 인자를 넣는다.};
    bless $self , $ class;
    return $self ;
}
 
 
 
Method
 
메소드를 정의할 수 있는데 첫번째 인자는 무조건 $self 가 된다.
 
use strict;
use warnings;
 
package Person;
sub new
{
    my $class = shift ;
    my $self = {
        _firstName => shift ,
        _lastName  => shift ,
        _ssn       => shift ,
    };
    # Print all the values just for clarification.
    print "First Name is $self->{_firstName}\n" ;
    print "Last Name is $self->{_lastName}\n";
    print "SSN is $self->{_ssn}\n";
    bless $self , $ class;
    return $self ;
}
sub setFirstName {
    my ( $self, $firstName ) = @_ ;
    $self ->{_firstName} = $firstName if defined($firstName);
    return $self ->{_firstName};
}
 
sub getFirstName {
    my( $self ) = @_;
    return $self ->{_firstName};
}
1;
 
 
my $object = new Person( "Sam", "Kim" , 233222);
my $firstName = $object ->getFirstName();
print $firstName ;
 
 
 
Inheritance And override
 
아래 빨간색으로 칠해 놓은 부분을 잘 보자.
 
use strict;
use warnings;
 
package Person;
sub new
{
    my $class = shift;
    my $self = {
        _firstName => shift ,
        _lastName  => shift ,
        _ssn       => shift ,
    };
    # Print all the values just for clarification.
    print "First Name is $self->{_firstName}\n" ;
    print "Last Name is $self->{_lastName}\n";
    print "SSN is $self->{_ssn}\n";
    bless $self , $ class;
    return $self ;
}
sub setFirstName {
    my ( $self, $firstName ) = @_ ;
    $self ->{ _firstName} = $firstName if defined($firstName);
    return $self ->{ _firstName};
}
 
sub getFirstName {
    my( $self ) = @_;
    return $self ->{ _firstName};
}
1;
 
package Employee;
use strict;
 
# inherits from Person
our @ISA = qw(Person);    
 
# Override constructor
sub new {
    my ($class) = @_ ;
 
    # Call the constructor of the parent class, Person.
    my $self = $class-> SUPER::new( $_[ 1], $_[ 2], $_[ 3] );
    # Add few more attributes
    $self->{ _id}   = undef ;
    $self ->{ _title} = undef;
    bless $self , $ class;
    return $self ;
}
 
# Override helper function
sub getFirstName {
    my( $self ) = @_;
    # This is child class function.
    print "This is child class helper function\n" ;
    return $self ->{ _firstName};
}
 
# Add more methods
sub setLastName{
    my ( $self, $lastName ) = @_ ;
    $self ->{ _lastName} = $lastName if defined($lastName);
    return $self ->{ _lastName};
}
 
sub getLastName {
    my( $self ) = @_;
    return $self ->{ _lastName};
}
 
1;
 
 
my $object = new Employee( "Mohammad", "Saleem" , 23234345);
my $firstName = $object -> getFirstName();
print $firstName
 
 
Exercise
 
use strict;
use warnings;
 
package MyClass;
 
sub new
{
       print "MyClass::new Called\n";
       my $type = shift;
       my $self = {};
       return bless $self , $ type;
}
 
sub DESTROY
{
       print "MyClass::DESTROY Called\n"
}
 
sub myMethod
{
       print "MyClass::MyMethod called\n";
}
 
#################################################
 
package MySubClass;
 
our @ISA = qw(MyClass);
 
sub new
{
       print "MySubClass::new called\n";
       my $type = shift;
       my $self = $type -> SUPER::new;
       return bless $self , $ type;
}
 
sub DESTROY
{
       print "MySubClass::DESTROY\n";
}
 
sub myMethod
{
       my $self = shift;
       $self -> SUPER::myMethod();
       print "MySubClass::MyMethod called!\n"
}
 
#################################################
 
package main;
 
my $myObject = MyClass->new ();
$myObject-> myMethod();
 
my $mySubObject = MySubClass->new ();
$mySubObject-> myMethod();
  
 
 
 
 

'Script > Perl' 카테고리의 다른 글

17. Module  (0) 2020.01.21
15. Regular Expression 1 - 기본  (0) 2020.01.21
13. Sub Routine  (0) 2020.01.21
12. Reference  (0) 2020.01.21
11. Context  (0) 2020.01.21