Herbert Breunung > Perl6-Doc > Perl6::Overview::Object

Download:
Perl6-Doc-0.36.tar.gz

Annotate this POD

CPAN RT

Open  0
View/Report Bugs
Source  

NAME ^

Perl6::Overview::Object - Object-oriented Programming

DESCRIPTION ^

Basic class definition

    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.
    }

Inheritance

    class ParentClass {...}

    class SubClass is ParentClass {...}
    # or
    class SubClass {
        is ParentClass;
        ...;
    }

Methods

    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

Roles

    role RoleName {
        method methodname(...) {...}
    }

    class SomeClass does RoleName {...}
    # or
    class SomeClass {
        does RoleName;
    }

Magical function

    self    # Returns the current instance

Magical variables

    $?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

Subtypes

    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
syntax highlighting: