Perl 6 classes will autogenerate accessors that return the underlying attribute itself as an lvalue:
class DogTag { has $.name is public; has $.rank is public; has $.serial is readonly; has @.medals is public; submethod BUILD ($id) { $.serial = $id }
method title () { return "$.rank $.name" } }
my $grunt = DogTag.new(71625371);
$grunt.name = "Pyle"; $grunt.rank = "Private";
print "$grunt.title ($grunt.serial)\n";
And Perl 6 supports "chaining" of accessors using $_ and the unary dot operator, not return values:
given $grunt { .name = "Patton"; .rank = "General"; push .medals, "Purple heart"; }
The closest equivalent Perl 5 would be:
package DogTag; sub new { my ($class, $id) = @_; bless { name=>undef, rank=>undef, serial=>$id, medals=>[] }, $class; }
sub name : lvalue { $_[0]{name} } sub rank : lvalue { $_[0]{rank} } sub serial { $_[0]{serial} } sub medals { $_[0]{medals} }
sub title { return "$_[0]{rank} $_[0]{name}" }
package main;
my $grunt = DogTag->new(71625371);
$grunt->name = "Pyle"; $grunt->rank = "Private";
print $grunt->title, " (", $grunt->serial, ")\n";
And for "chaining":
for ($grunt) { $_->name = "Patton"; $_->rank = "General"; push @{$_->medals}, "Purple heart"; }
Damian