4/4/2023 0 Comments Perl tutors![]() We sometimes say that an object has been "blessed into a class". A blessed data structure (aka "a referent") is an object. While we strongly suggest you don't build your objects from scratch, you should know the term bless. Turning a plain data structure into an object is done by blessing that data structure using Perl's bless function. (Don't worry about that -> operator, it will be explained later.) #BlessingĪs we said earlier, most Perl objects are hashes, but an object can be an instance of any Perl data type (scalar, array, etc.). However, most OO modules on CPAN use a method named new() to construct a new object: my $hostname = File->new( In Perl, there is no special keyword for constructing an object. Here's our "class declaration" for the File class: package File The difference between a package which is a class and one which isn't is based on how the package is used. A specific object is often referred to as an instance of a class. When we want to create a specific object, we start with its class, and construct or instantiate an object. For example, our /etc/hostname object belongs to the File class. A class is a name for a category (like "File"), and a class also defines the behavior of objects in that category.Īll objects belong to a specific class. #ClassĪ class defines the behavior of a category of objects. ![]() In practice, it's best to consider an object's internal data structure opaque. In Perl most objects are hashes, but the OO systems we recommend keep you from having to worry about this. The methods associated with a file might include rename() and write(). If we created an object to represent /etc/hostname on a machine named "", that object's path would be "/etc/hostname", its content would be "foo\n", and it's last modification time would be 1304974868 seconds since the beginning of the epoch. The attributes for a file object might include its path, content, and last modification time. For example, an object might represent a file. An object can be thought of as a noun (a person, a web service, a computer).Īn object represents a single discrete thing. An object's data is called attributes, and its subroutines are called methods. #ObjectĪn object is a data structure that bundles together data and subroutines which operate on that data. JavaScript's OO system is prototype-based. JavaScript is the most popular language to use another paradigm. There are other object orientation paradigms as well. It's used by Java, C , C#, Python, Ruby, and many other languages. If you're already familiar with these terms, you should still skim this section, since it explains each concept in terms of Perl's OO implementation. Understanding the concepts will make it much easier to read and write object-oriented code. You've probably heard terms like "class", "object, "method", and "attribute" before. Most object systems share a number of common concepts. You should also read the perlsyn, perlop, and perlsub documents. If you don't understand these concepts yet, please read perlintro first. This document assumes that you already understand the basics of Perl syntax, variable types, operators, and subroutine calls. If you want to know how Perl OO works under the hood, the perlobj document explains the nitty gritty details. Fortunately, Perl's flexibility has allowed a rich ecosystem of Perl OO systems to flourish. This minimalism made a lot of sense in 1994, but in the years since Perl 5.0 we've seen a number of common patterns emerge in Perl OO. Then it introduces several different OO systems from CPAN which build on top of what Perl provides.īy default, Perl's built-in OO system is very minimal, leaving you to do most of the work. It begins with a brief overview of the concepts behind object oriented design. ![]() ![]() This document provides an introduction to object-oriented programming in Perl. We recommend you start by reading the perlootut document in the latest stable release of Perl, rather than this version. If you are reading this in the future then it's possible that the state of the art has changed. This document was created in February, 2011, and the last major revision was in February, 2013. Perlootut - Object-Oriented Programming in Perl Tutorial #DATE
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |