This is a note about chap03 in “Object oriented perl”.
1.In object oriented perl, there are three rules,
1 2 3
2.Some cases while calling a method through an arrow:
1 2 3 4
3.When a method like
Bug::print_me is called, the argument list that it receives begins with the object reference through which it was called, followed by any arguments that were explicitly given to the method. That means that calling
is not the same as calling
In the first case, print_me is treated as a regular subroutine so the argument list passed to
Bug::print_me is equivalent to:
In the second case,
print_me is treated as a method so the argument list is equivalent to:
Having a reference to the object passed as the first parameter is vital, because it means that the method then has access to the object on which it’s supposed to operate. Hence you’ll find that most methods in Perl start with something equivalent to this:
1 2 3 4
or better still:
1 2 3 4
This second version is better because it provides a lexically scoped copy of the argument list
4.Unlike other object-oriented languages, Perl doesn’t require that an object be a special kind of recordlike data structure. In fact, you can use any existing type of Perl variable—a scalar, an array, a hash—as an object in Perl.
bless function takes two arguments: a reference to the variable to be marked and a string containing the name of the class. It then sets an internal flag on the variable, indicating that it now belongs to the class.
6.We didn’t bless the reference; we blessed the referent. The scalar didn’t change—only the nameless hash it refers to has been marked.
7.You can check that the blessing succeeded by applying the built-in ref function to
$nextbug. Normally, when
ref is applied to a reference, it returns the type of that reference.
1 2 3 4 5 6 7
THe above code gives out the result,
8.Based on the things we said above, we will show a piece of code to demonstrate how to get and set attributes in perl.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42