Learn how to build a fully-functional module.
- [Voiceover] In this lesson, I'll take you through a more functional version of our simple class. Let's get started, here's a working copy of hello.pl from Chapter 14 of the exercise files. And I'm just going to come down here and I'm going to say use better, BW, colon, colon, better. And I'll create an object, and I'll go ahead and get a value from it, and I'll display that value. And when I run this, I did that wrong, dollar O number, like that, and when I run this we get our value.
So if we come over here to our better class, again it's package BW better, because it's in the BW folder, and we have the version, and we have our constructor. Our constructor is a little bit refined, and here's how this works, when the constructor is called the name of the class is passed to it. I'm putting this in this inv variable, which to me stands for invocation. And so I know the name of the class, and I take that name of the class and I assign it to a variable called class, and it can either be a string or it can be a reference, depending on how the constructor is called.
And again, that's more detail than we need here. This syntax, this structure, is standard and this works, I get the class name this way, and I set up my, down here in line 14, I set up my hash for our object data, and I call bless with those two arguments. And again, all that does, I mean, it's a lot of details, but for what we need to understand, is this is how we turn this module into a class that can be used to create objects.
And now when I pass in a number, or a zero, if there's no number on the argument array, and I return self, and returning self is the actual blessed object, and that return then gets put in this scale or variable, because it's a reference to that object. And from the perspective of the caller, it actually looks just like a reference to a hash, although if I say dollar O like this, you'll notice that it says hash, just like that, it just looks like a hash, although it has the name of the class in it, and so that's because it's blessed as an object.
So a couple of other things that we've done here that are a little bit different than last time. The number is now both a setter and a getter, so I can change the number here by saying, let's just go ahead and duplicate this, and rather than that I can say number and give it a new value 183, and when I run this it says dollar X equals and now the value of X is 183.
So the way that this works is really simple. After getting self, I get the number and I simply have a postfix if, if there's anything left on the default array, if there isn't then we don't set the number and we simply get the value. And so I can just get the value like this, I can say dollar X equals dollar O number, like that, and that second time it's just getting the value, and you notice we get exactly the same result.
So this number is now both a setter and a getter. Now we have a function called better, which you remember from one of our earlier examples, I have a silly little program that I like to write that gives you a better number, you give it a number and it gives you a better number. This calls make_better and you notice that it calls it with self and dereference, and the make_better function, and so that calls this function down here, and it takes that number from the getter, and if it doesn't get a number it takes six, and it adds it to a random number, and it returns a better number than the number that you've provided.
And again it's just an example to show that we can do some processing here in the class, and how we call functions and like that, and then it returns that number. So if I come over here and I say a better number is and dollar O better, like that, and when I call that it says a better number is 189, and if I do this with our previous number, that's a better number than 47. And string is the same as before, and version is the same as before, so what we have here is a functioning class.
And you can see that it's very easy to do. It may seem a little bit odd if you're used to other object-oriented languages, but again it's just very simple, and it makes object-oriented programming possible and simple here in Perl. So this is just a quick example of a fully functioning Perl module, most modules aren't really much more complicated than this, of course you can use these techniques to create as much complexity as you desire.
Watch to learn the details of the Perl syntax, from variables, conditionals, loops, and data structures to regular expressions, functions, and references. A quick-start guide is included for experienced developers who want to get up and running with Perl 5 fast, and the entire course is recommended for both new and experienced programmers alike. Later chapters cover file handling and reusing code with Perl modules, plus Perl best coding practices.
- Understanding Perl's general syntax and the anatomy of a Perl script
- Writing statements and expressions
- Creating assignments
- Working with variables and strings
- Using data types effectively
- Defining logical flow with conditionals and loops
- Using special variables
- Using Perl operators
- Performing simple Perl programming tasks with expressions
- Matching data
- Defining and calling functions
- Using references
- Handling files in the file I/O
- Using built-in functions
- Reusing code with modules
- Coding with Perl best practices
Skill Level Intermediate
Programming Foundations: Refactoring Codewith Simon Allardice1h 44m Intermediate
1. Setting Up
About Perl3m 36s
2. Quick Start
3. Basic Syntax
4. Values and Variables
7. Special Variables
9. Regular Expressions
11. References and Structures
12. File I/O
13. Built-In Functions
15. Best Practices
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.