- Perl ›
- Object Oriented ›
- Syntax ›
- package
Create class - package
I will explain how to create a class.
1. Make a package declaration to create a class.
There is no special syntax for creating classes in Perl. Create a class by making a package declaration .
package Book;
This completes the Book class.
2. Class template constructor and accessor
The template for the monotype class looks like this. It consists of a constructor for creating the object and a accessor for accessing the fields of the object.
package Book;
sub new {# Constructor implementation}
sub title {# Implementation of accessor}
sub author {# Implementation of accessor}
In the case of a human type class, it looks like this.
package File Parser;
sub new {# Constructor implementation}
sub encoding {# Implementation of accessor}
sub err_raise {# Implementation of accessor}
sub parse {
# Implementation of method to parse file
}
The class starts from this form. Next time, I will explain the implementation of the constructor.
Perl ABC