Since I am new to perl and here are some notes for writing perl in OOP without using package without using Moose module.
It is recommended to include
1 | use strict; |
at the very beginning of your code to make your code easier for others to read and understand.
So class in perl is basically a module. With the constructor subroutine and bless
, it is easy to act like a class.
First of all, put your class in the file <classname>.pm . It is the module for the implementation of your class.
This module will have a constructor and some methods:
1 | package <classname>; |
The constructor subroutine is not necessary to be named new
.
In your perl file, after include this module with use <classname>;
, you could create a new class instance in the following way and call its methods:
1 | my $instance = <classname> -> new ( \%args ); |
For above codes, an class instance is created by new ( \%args );
. It returns a handler and store in $instance
. Actually, it is a hash in perl. All subroutines can be called in the way of $classname -> subroutineName()
like methods of class. All the attributes are stored in the hash, which is binded to $class
by bless
. You can directly access the attributes by $class -> {$key}
, which is not recommended.
That is all about writing perl in OOP.