You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
939 B
Perl

There is no "class" keyword.
The properties are variables used to describe the object.
Methods are functions that create and manipulate the object.
Objects are created with the bless function.
#Creating a Class
package Pet
sub new{ # Constructor
my $class = shift;
my $pet = {
"Name" => undef,
"Owner" => undef,
"Type" => undef,
};
bless($pet, $class);
sub set_pet{
my $self = shift;
my ($name, $owner, $type)= @_;
$self->{'Name'} = $name;
$self->{'Owner'}= $owner;
$self->{'Type'}= $type;
}
sub get_pet{
my $self = shift;
while(($key,$value)=each($%self)){
print "$key: $value\n";
}
}
#Instantiating a Class
$cat = Pet->new();
# Create an object with a constructor method
$cat->set_pet("Sneaky", "Mr. Jones", "Siamese");
# Access the object with an instance
$cat->get_pet;