The main perl way:
In the file "Foo.pm":
use strict; use warnings; package Foo; sub new { my $class = shift; my $self = bless {}, $class; my %args = @_; $self->{_message} = $args{message};
In the script:
use Foo; my $foo = Foo->new(message => "Hello world"); $foo->hello();
You can use Moose, but in this case the Foo.pm file:
package Foo; use Moose; has message => (is => 'rw', isa => 'Str'); sub hello { my $self = shift; print $self->message, "\n"; } 1;
Because Mus makes all the accessories for you. Your main file is exactly the same ...
Or you can use Moose extensions to make everything prettier, in which case Foo.pm becomes:
package Foo; use Moose; use MooseX::Method::Signatures; has message => (is => 'rw', isa => 'Str'); method hello() { print $self->message, "\n"; } 1;
source share