Tuesday, February 24, 2015

Perl property method?

package Foo

sub new
{
    ...
}


# bar property
sub bar
{
    my ($self, $text) = @_;
    if ($text)
    {
        $self->{bar} = $text;
    }
    else
    {
        return $self->{bar};
    }
}

1;

my $foo = Foo->new;

$foo->bar; # returns value of $self->bar
$foo->bar("baz"); # sets $self->bar = "baz"