Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Foo.pl
- package Foo;
- sub new {
- my ($class, @args) = @_;
- my $self = bless({}, $class); # bless is a magic function that takes an object (int, string, hash, ...) and makes it of class $class
- $self->{name} = "Alice";
- return $self;
- }
- sub name { # this is like descriptor, called on #obj->name
- my $self = shift;
- if (@_) {
- my $new_name = shift;
- $self->{name} = $new_name;
- }
- return $self->{name};
- }
- sub say {
- my $self = shift;
- print "Hi, I'm $self->{name}";
- }
- # main.pl
- use Foo;
- my $foo = Foo->new;
- $foo->name("Bob");
- $foo->say; # prints "Hi, I'm Bob
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement