ObjectsHere is a quick and dirty overview of objects generally, and Perl objects specifically.First of all, these command-line tutorials are highly recommended: perldoc perltoot perldoc perltooc perldoc perlobj What is OO?Object-oriented (OO) programming is a way of encapsulating data and providing a consistent interface to access and modify that data. "perldoc perltoot" says: "An object is nothing but a way of tucking away complex behaviours into a neat little easy-to-use bundle."Let's use an ATM as an example. It provides a consistent interface (the touch screen and your ATM card) for you to perform banking transactions. You, the end-user, don't know exactly HOW the ATM implements the various functions (like get_account_balance(), withdraw_cash(), update_account(), etc), but you know that you can insert your ATM card, type in your PIN, and perform transactions. This is a good thing, because it means that the ATM company engineers can change the underlying implementation at any time (fix software bugs, improve mechanical parts, switch to a new operating system) as long as the interface stays the same. You, the end-user, simply continue to use that interface, and don't know or care about the implementation. (the fancy way to say this is we have "decoupled the interface from the implementation". it's important to understand the difference between interface and implementation, so I recommend thinking that one over for a second or two.) Objects work the same way. If we translate our ATM example to software, each ATM would be a single object. It's important to understand that an object is a "container" for some data. An object is not data, it's something that provides access to data. We could imagine creating functions for each ATM called get_account_balance(), withdraw_cash(), transfer_money(), deposit_funds(), etc. The question is, how does an object know which functions to trust? What if we tried to call a function called give_me_free_money() on our ATM object? The answer is: every object belongs to a class, which is the blueprint that lays out what functions are available. In OO terminology, the functions we talked about in the last paragraph are called methods. So, the class specifies the valid methods for the object. There is a lot of lingo related to OO programming. You don't have to know all of it, but in addition to class and method, you should understand:
How do I do OO in Perl?You learned 2 ways to create Perl objects last quarter, building them by hand and using the Class::Std module. Class::Std is a great module, automating some of the more tedious aspects of creating Perl classes, and it also offers some additional privacy/security, but it isn't required for this project.To create a class, write a package. (It's highly recommended to have one package per file.) If you want to create a class called ATM, for example, you'd create a file called ATM.pm, and the first line in the file would be "package ATM;". To create a method, write a subroutine. In perl, methods are simply subroutines that belong to a class (to a particular package). The only difference between a non-OO subroutine in a package and a method is that the method will expect an object reference or package name as the first argument to the subroutine. We'll get into that more in a minute. To create an object, call "bless" on a reference. Most Perl objects are implemented as hash references (and I recommend this for the final project), but you're free to choose any type of reference. Many people do not understand what "bless" does. Its job is simply to take a reference (say, a hash reference) and mark it as belonging to a class. The reference is STILL a reference after it has been blessed, it simply is now branded, like a cow, as a member of a class (an "ATM", for example). Note the special behavior of the arrow operator ("->"). We used it with complex data structures to mean "de-reference". With objects and methods, it's similar. However, there is an important difference. Say we have a Perl class called "ATM". We could then do: use strict; use ATM; my $atm = ATM->new; # create new objectWhen you use the arrow operator with a method (like "new"), Perl calls a subroutine called "new". The question is, where does Perl find this subroutine? If the thing on the left of the arrow is a string (like "ATM") then that is the package name where Perl will find the subroutine. In other words, Perl will call "ATM::new()". If the thing on the left of the arrow is an object, then Perl first figures out the class of that object, and then calls that package. So if we have $atm->deposit_funds(id => 8, amount => '29.95');then Perl would call "ATM::deposit_funds()", because $atm is an object belonging to the class "ATM". There is one final piece to the arrow operator behavior, however. In addition to calling the subroutine on the right of the arrow, Perl passes the thing on the left of the arrow as the first argument to the subroutine! Using the above examples, with "ATM->new", Perl will actually call "ATM::new('ATM')". And with the deposit_funds example, Perl will actually call "ATM::deposit_funds($atm, id => 8, amount => '29.95')". Here's a complete example, a class representing a truck:
#------------------------------------------------------------------------
# Each Truck object is a blessed hash with attributes like this:
#
# $self = {
# make => 'chevy',
# year => 1995
# }
#------------------------------------------------------------------------
package Truck;
use strict;
# The constructor method
sub new {
# Normally $class will be 'Truck' (if you're curious, inheritance
# could cause this to change)
my $class = shift;
my $self = {};
bless($self, $class);
return $self;
}
sub register {
my ($self, %args) = @_;
$self->{make} = $args{make};
$self->{year} = $args{year};
}
# Accessors to get truck make and year
sub get_make {
my $self = shift;
return $self->{make};
}
sub get_year {
my $self = shift;
return $self->{year};
}
1;
And here is the calling script that would use the Truck class:
use strict;
use Truck;
# create 2 new Truck objects
my $chevy = Truck->new;
my $nissan = Truck->new;
# register the Trucks (that is, call the register method on each Truck object)
$chevy->register(make => 'Chevrolet', year => 1995);
$nissan->register(make => 'Nissan', year => 2007);
print "Hi, I have 2 trucks:\n\n";
# iterate over the truck objects and print out their attribute values
foreach my $truck ($chevy, $nissan) {
my $make = $truck->get_make;
my $year = $truck->get_year;
print "One is a $make truck, made in $year\n";
}
| ||
|
UW Extension Perl Programming Course Three, Perl, the Web, and Databases March 26 - June 04, 2007 (note no class May 28) Monday evenings 10 Sessions, 6:00 to 9:00 PM Instructor: Joel Grow (joelg at u.washington.edu) |
links: perl.com cpan perldoc online learn.perl.org perlmonks.org seattle perl users group (spug) |
|
|
Thursday, May 24, 2012 you're number: 687 | ||