Perl6::Overview::Object - Object-oriented Programming
class ClassName { has $.public_instance_var; # Untyped instance variable, a read-only accessor is automatically # generated. Methods have read-write access to $.instance_var. has $.public_instance_var is rw; # Untyped instance variable, a read-write accessor is automatically # generated. has Type $.public_instance_var; # Typed instance variable. has $!private_instance_var; # Private instance variable, no accessor is automatically generated. }
class ParentClass {...} class SubClass is ParentClass {...} # or class SubClass { is ParentClass; ...; }
method methodname(...) {...} # Standard subroutine signature method methodname($invocant: ...) {...} # The invocant (the class or the instance) is explicitly bound to # $invocant. In any case, there's self and $?CLASS, too. method ^methodname(...) {...} # Class method method methodname(::?CLASS $self: ...) {...} # Instance method
role RoleName { method methodname(...) {...} } class SomeClass does RoleName {...} # or class SomeClass { does RoleName; }
self # Returns the current instance
$?CLASS # The current class as scalar variable ::?CLASS # The current class as package variable $?ROLE # The current role as scalar variable ::?ROLE # The current role as package variable
my subtype Int::Odd of Int where { $^value % 2 == 1 }; # Declaration of a new, lexical subtype of Int, allowing only odd integers. my Int::Odd $var = 3; # valid $var++; # invalid my Int::Odd $var = 4; # invalid
To install Perl6::Doc, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Perl6::Doc
CPAN shell
perl -MCPAN -e shell install Perl6::Doc
For more information on module installation, please visit the detailed CPAN module installation guide.