You now know how to instantiate a new class by using a new() function and how to create class properties (the class information) with undefined values. Let's look at how to give those properties some real values. You need to start by looking at the new() function from the first example in this chapter init.pl:
sub new { my($class) = shift; bless { "PART_NUM" => undef, "QTY_ON_HAND" => undef }, $class; }
The new() function is a static method. Static methods are not associated with any specific object. This makes sense because the new() function is designed to create objects. It can't be associated with an object that doesn't exist yet, can it?
The first argument to a static method is always the class name. Perl takes the name of the class from in front of the -> operator and adds it to the beginning of the parameter array, which is passed to the new() function.
If you want to pass two values into the new() function to initialize the class properties, you can modify the method to look for additional arguments as in the following init2.pl:
sub new { my($class) = shift; my($partNum) = shift; my($qty) = shift; bless { "PART_NUM" => $partNum, "QTY_ON_HAND" => $qty }, $class; }
Each parameter you expect to see gets shifted out of the parameter array into a scalar variable. Then the scalar variable is used to initialize the anonymous hash.
You invoke this updated version of new() by using this line of code:
$item = Inventory_item->new("AW-30", 1200);
While this style of parameter passing is very serviceable, Perl provides for the use of another technique: passing named parameters.