|
In the next few lectures we are going to learn how to write Object Oriented code in Perl. We will start with some simple examples and go deep step by step. We start using Moose but we'll also learn how to create classes in other ways. Let's start by writing a simple script that uses the Person class. We don't do anything special yet, just load the module and call the constructor to create an instance. use strict; use warnings; use v5.10; use Person; my $teacher = Person->new; This should not be new to you as I am sure you have already used other modules in a similar way. Our focus is how the Person class was implemented: package Person; use Moose; 1; That's it. This code is saved in Person.pm. All you need to do to create a class is to create a package with the name of the class, add use Moose; to it, end the file with a true value and save it in a file with the same name (case sensitive!) as the package, and with .pm extension. Loading Moose automatically sets up use strict and use warnings. This is nice though it might cause some people to forget them in non-Moose code. Loading Moose also automatically adds a default constructor called new. (As a side note, it is not a requirement in Perl that the constructor will be called new, but in most cases that's what the author chooses anyway.)
Attributes and accessorsHaving an empty class is not much fun. Let's go further in our use:
use strict;
use warnings;
use v5.10;
use Person;
my $teacher = Person->new;
$teacher->name('Foo');
say $teacher->name;
In this code after creating the object we call the name method with a string as a parameter, setting the name attribute of the class to be 'Foo'. Because this method sets the respective attribute it is also called setter. Then, we call the same method again, this time without any parameter. That will fetch the value previously stored. Because this gets the value this is also called getter. In our case the getter and the setter has the same name but it isn't a requirement either. In general getters and setters are called accessors. The code implementing the new class is this: package Person; use Moose; has 'name' => (is => 'rw'); 1; The new part, has 'name' => (is => 'rw'); say that "The Person class has an attribute called 'name' which is readable and writeable" This automatically create a method called name which becomes both a setter (for writing) and a getter (for reading).
Try the codeIn order to try this create a directory called "P", inside create a subdirectory called "lib" and inside save the Person.pm file. Also create a subdirectory called "script" and save the script there called person.pl. You should have P/lib/Person.pm P/script/person.pl Open your terminal (or cmd window on Windows) change the directory to be in "P" and type perl -Ilib script/person.pl (On MS Windows you might need to use back-slashes: \ )
Constructor parametersIn the next script we pass a key-value pair to the constructor, corresponding to the name of the attribute and its value. use strict; use warnings; use v5.10; use Person; my $teacher = Person->new( name => 'Foo' ); say $teacher->name; This works too with the same module as we already have. Moose automatically accepts every member (another name for attributes) to be passed during construction.
ExerciseAs an exercise, first of all follow the instructions above. Save both the module and the script and run the script. It should print "Foo". Then add another call to set the phone number of Foo. Observe the error message. Then add the appropriate attribute and see that it works now.
Perl tutorial and video courseFor further articles see the Perl tutorial or, if you'd like to hear and see the explanation on video, check out the Advanced Perl Maven online video course.In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation. blog comments powered by Disqus |
Follow me: