I am new to Perl, and I would like to understand/know more about the OO parts. Say I have a "class" with only attributes; are there benefits/advantages for creating a package and blessing a hash over working on a hash directly?
for simplicity, lets consider the following example :
package Person;
sub new {
my $class = shift;
my $args = {Name => '', Age => 0, @_};
my $self = { Name => $args->{Name},
Age => $args->{Age}, };
bless $self, $class;
}
package main;
my $person1 = Person->new(Name => 'David', Age => 20);
my $person2 = {Name => 'David', Age => 20, Pet => 'Dog'};
print $person1->{Name} . "\n";
print $person2->{Name} . "\n";
What I would like to know is what is the difference between $person1
and $person2
, beside the OO parts, beside the fact that 1 is a blessed hash and 2 is a hash reference?
Are there any benefits of working with an object, in this case, over working on a hash?
After reviewing the answers :
Thanks for all the help :)
Håkon Hægland comment has the closest answer for me, I was just wondering, consider I only have to hold, simple scalars, no special checks, no other functionality, are there benefits for a class over a simple hash (I understand that if I need extra functionality and inheritance a class will be the right tool)