BernieC has asked for the wisdom of the Perl Monks concerning the following question:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: OO modules
by stevieb (Canon) on Dec 28, 2020 at 18:04 UTC | |
An OO module *is* a library. The words module and library can be used for each other interchangeably. For OO, you must have a module/library file. This file contains the "class" that your objects will be blessed into. To make things more confusing, a class is called a "Package" in Perl. In the below example, you'll see what I mean. new() is a class method that instantiates, configures and returns an object of the class. In its most basic form, it can look like this: sub new {bless {}, shift;}. That blesses a hash reference into the class, and implicitly returns the new object. You can use any name for this method as you like, but the de-facto unwritten rule standard is and always has been new(). I know some devs who use init() or other things, but because of the unforced standard, I always expect to see a new() method if I'm looking at someone elses code. bless() is the class method that actually makes a standard perl reference type an object of your class. It provides you the ability to call methods against it etc. This method is internal to perl, and can not be changed. You can bless any reference type you want. The de-facto standard is a hash reference, but there are a few reasons why you'd want to use other types. Hashes make it easy to use the attributes/members of the object (example code below unverified and untested):
Script:
$self is the actual object itself. We (pretty well) always refer to an object of the class as self while working with it within the class. Again, this is not a hard rule, but just stick with it so others reading your code won't be confused. When you call an object's method, the object is inserted at the beginning of the parameter list into the method. This happens automatically behind the scenes. So this:
Is converted to:
When our swear() method is called, we shift off the object and the swear word into the variables $self and $word. We're then able to call other methods or do other work on the $self object. Here's what the above looks like when I display the contents of the @_ array in the method. The @_ array contains the full parameter list to the method:
Another note... methods that begin with an underscore are "private" methods. Ones that typically do maintenance and validation type work, or are chunks of larger public methods that perform specific pieces of functionality. These methods are not designed for use by the users of your objects. An example is the _validate_args() in the above example. This rule is also unenforced, but is a very long-standing standard. Try not to use common names for these, use long, descriptive ones. | [reply] [d/l] [select] |
by shmem (Chancellor) on Dec 29, 2020 at 09:09 UTC | |
An OO module *is* a library. The words module and library can be used for each other interchangeably. For OO, you must have a module/library file. This file contains the "class" that your objects will be blessed into. To make things more confusing, a class is called a "Package" in Perl. To nitpick a bit on that: it is in fact the other way round. First comes the namespace which is called a package. It is declared with the keyword package followed by the package name, optionally followed by a block containing all what belongs to that namespace. Without that block which defines it, the packages scope ends when another namespace (or package) is declared, when the enclosing scope ends or at end of file. This namespace can be OO, but must not be. Any number of packages may be declared in whatever perl code, and packages can be switched within. For any collection of functions to be a module or library, OO or not, it must be contained in its own file - otherwise it is not modular. When useing a module via its namespace (package) name, it must be contained in a file relative to any directory in perls include path, where its relative path is the package name with .pm appended, and the double colons :: within are substituted by the directory path separator. But require provides more rope. So, package, module and OO - or namespace, library and namespace aware function collections - are loosely coupled concepts in the sense that each can be used on its own. It is perfectly feasible to write a program which uses OO but doesn't use any module, where all classes are lumped together into one file - in which case it isn't modular. This is what App::FatPacker does. The path resolution thingy of included files is a different thing, explained in use and require. A package directive within isn't mandatory if it isn't OO, its last statement must just yield some true value:
This, while being useless, compiles and adds a key/value pair to %INC but doesn't create/add the namespace Useless::Nothing. Any symbols defined in such a file without explicit package directive are hooked into the package in scope where the use occurs. Oh, and bless() is the class method that actually makes a standard perl reference type an object of your class. bless() is a function, and a method of package CORE. So to work as a method, the invoking variable needs to be blessed into package CORE first...
Which also means that you can have a class method bless, but that's somewhat useless, except for obfuscation and other sophisticated use cases:
That would be a method for object migration ;)
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
| [reply] [d/l] [select] |
|
Re: OO modules
by Discipulus (Canon) on Dec 28, 2020 at 20:47 UTC | |
you can be interested in some links I collected in my bibliotheca L*
There are no rules, there are no thumbs.. Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS. | [reply] [d/l] |
by perlfan (Parson) on Dec 28, 2020 at 22:51 UTC | |
| [reply] |
|
Re: OO modules
by ikegami (Patriarch) on Dec 28, 2020 at 18:38 UTC | |
Perl assigns no special meaning to new. bless associates a hash* with a class, allowing $o->method to work.
| [reply] [d/l] [select] |
by BernieC (Pilgrim) on Dec 28, 2020 at 19:35 UTC | |
| [reply] |
by ikegami (Patriarch) on Dec 29, 2020 at 17:43 UTC | |
hmmm? Where files are located has no bearing on how to create an object. | [reply] |
|
Re: OO modules
by perlfan (Parson) on Dec 28, 2020 at 17:44 UTC | |
Update: Forgot to mention, Object Oriented Perl by Damian Conway. It's excellent for that and a lot more. | [reply] |
by stevieb (Canon) on Dec 28, 2020 at 18:32 UTC | |
I got my feet wet back in 2002 or so with Randal Schwartz's (merlyn) "Learning Perl Objects, Reference and Modules". It's been years since I've seen the book, but I'm pretty sure that if you found a copy to dust off, it'd be relatively useful to this day. | [reply] |