Remember the concept of references that was discussed previously.
References will play a large role in the rest of the chapter and are critical to understanding how classes are used. You specifically need to remember that the { } notation indicates an anonymous hash. Armed with this knowledge and the object-oriented terminology from the first part of this chapter, you are ready to look at real Perl objects.
The following listing shows how the inventory_item class could be defined in Perl:
The Perl code for defining the Inventory_item Class is:
package Inventory_item; sub new { my($class) = shift; bless { "PART_NUM" => undef, "QTY_ON_HAND" => undef }, $class; } package main; $item = Inventory_item->new();
There is a lot of new stuff in this small ten-line listing. terminology.
The first line, package Inventory_item; says two things, depending on if you are thinking in terms of objects or in terms of Perl. When considering objects, it begins the definition of a class. When considering Perl, it means that a specific namespace will be used.
You read a little bit about namespace in Chapter 3 "Variables." A namespace is used to keep one set of names from interfering with another. For example, you can have a variable named bar and a function called bar, and the names will not conflict because variables and functions each have their own namespace.
The package keyword lets you create your own namespace. This lets you create more than one function called new() as long as each is in its own package or namespace. If you need to refer to a specific function in a specific namespace, you can use Inventory_item->new, Inventory_item::new, or Inventory_item'new. Which notation you use will probably depend on your background. Object-oriented folks will probably want to use the -> notation.
The second line, sub new, starts the definition of a function. It has become accepted practice in the object-oriented world to construct new objects with the new() method. This is called the class constructor. This might be a good time to emphasize that the class definition is a template. It's only when the new() function is called that an object is created or instantiated. Instantiation means that memory is allocated from your computer's memory pool and devoted to the use of this specific object. The new() function normally returns a reference to an anonymous hash. Therefore, the new() function should never be called unless you are assigning its return value to a variable. If you don't store the reference into a scalar variable for later use, you'll never be able to access the anonymous hash inside the object. For all intents and purposes, the anonymous hash is the object.
Note Not all objects are represented by hashes. If you need an object to emulate a gas tank, perhaps an anonymous scalar would be sufficient to hold the number of gallons of gas left in the tank. However, you'll see that working with hashes is quite easy once you learn how. Hashes give you tremendous flexibility to solve programming problems.
There is nothing magic about the new function name. You could call the function that creates new objects create() or build() or anything else, but don't. The standard is new(), and everyone who reads your program or uses your classes will look for a new() function. If they don't find one, confusion might set in. There are so few standards in the programming business. When they exist, it's usually a good idea to follow them.
The bless() function on the third line changes the data type of its first parameter to the string value of its second parameter. In the situation shown here, the data type is changed to the name of the package, Inventory_item. Using bless() to change the data type of a reference causes the ref() function to return the new data type. This potentially confusing point is explained further in the section "Example: Bless the Hash and Pass the Reference" later in this chapter.
Note The bless() function is used without using parentheses to surround the parameters.
Embedded inside the bless() function call is the creation of an anonymous hash that holds the properties of the class. The hash definition is repeated here for your convenience:
{ "PART_NUM" => undef, "QTY_ON_HAND" => undef };
Nothing significant is happening here that you haven't seen before. Each entry in the hash is a different property of the class. For the moment, I have assigned the undefined value to the value part of the entries. Soon you'll see how to properly initialize them.
After the new() function is defined, there is another package statement: package main;
There is no object-oriented way to interpret this statement. It simply tells Perl to switch back to using the main namespace. Don't be fooled into thinking that there is a main class somewhere. There isn't.
A note of Caution: While you could create a main class by defining the new() function after the package main; statement, things might get to be confusing, so don't do it!
The last statement in the file is really the first line that gets executed. Everything else in the script has been class and method definitions. $item = Inventory_item->new();
By now, you've probably guessed what this statement does. It assigns a reference to the anonymous hash to $item. You can dereference $item in order to determine the value of the entries in the hash. If you use the ref() function to determine the data type of $item, you find that its value is Inventory_item.
Here are some key items to remember about objects in Perl:
$item = new Inventory_item;
or
$item = Inventory_item->new();
Both of these techniques are equivalent, but the -> style is preferred by object-oriented folks.