in reply to Single accessor/mutator idiom in Perl5, Perl6 and Python

I like using Attribute::Property for this.

# Perl 5 use Attribute::Property; package SomeClass; sub given_name : Property;
# Perl 6 class SomeClass { has $.given_name is public; }

Given a constructor called new, this makes the following code possible:

# Perl 5 my $object = SomeClass->new; $object->given_name = 5; print $object->given_name, "\n";
# Perl 6 my $object = SomeClass.new; $object.given_name = 5; print $object.given_name, "\n";

Compare $object->given_name =~ s/foo/bar/ to its "@_ > 1" equivalent: (my $temp = $object->given_name) =~ s/foo/bar/; $object->given_name($temp);.
Or something simple like $object->given_name++, which using the classic syntax would be $object->given_name($object->given_name + 1). It feels like BASIC to me :)

(Properties created with Attribute::Property do support the classic syntax).

Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }