(a) package Person; sub age { $_[0]=>{age} } sub new { my $self = shift; my ($name, $age) = @_; bless { name => $name, age => $age }, $self; } $p1 = Person->new('elmo', 4) $p2 = Person->new('zoe', 7) print $p1->age; |
(b) class Person; has $.age is public; method new ($name, $age) { $.name = $name; $.age = $age; return .bless; } $p1 = Person.new('elmo', 4) $p2 = Person.new('zoe', 7) print $p1.age; |
(c) class Person; attr :age; def initialize(name, age) @name = name @age = age.to_i end p1 = Person.new('elmo', 4) p2 = Person.new('zoe', 7) print p1.age; |
Example 1: (a) An example of object-oriented Perl 5; (b) the same code in Perl 6; (c) the same code in Ruby.