I have been studying OOP and Moose.
I stumbled upon the RJBS presentation
https://cdn.oreillystatic.com/en/assets/1/event/115/Moose%20is%20Perl_%20A%20Guide%20to%20the%20New%20Revolution%20Presentation%202.pdf
"Moose is Perl".
I find it instructive to write Perl 5 programs showing each example found in presentations, blogs, and tutorials.
Here is one:
Perl 5 program: mip-087-001.pl
#!/usr/bin/perl
# Program name: mip-087-001.pl Run it, thusly: See C++ comment below.
+ Moose is Perl (i.e., MIP or mip) presentation.
# From https://cdn.oreillystatic.com/en/assets/1/event/115/Moose%20is%
+20Perl_%20A%20Guide%20to%20the%20New%20Revolution%20Presentation%202.
+pdf.
# The code below is from https://www.perl.com/pub/2002/08/13/comment.h
+tml/
use Acme::Comment type => 'C++', one_line => 1, own_line => 0;
/*
cd C:\2020-Raku-001\
perl C:\2020-Raku-001\Perl-scripts\mip-087-001.pl
*/
use strict;
use warnings;
use feature 'say';
local $| = 1; # Forces a flush.
package Employee {
use strict;
use warnings;
use Moose;
use namespace::autoclean; # Code this instead of coding "no Moose;" a
+t the end of the package block.
has name => (
is => 'ro',
isa => 'Str',
required => 1,
);
has title => (
is => 'rw',
isa => 'Str',
required => 1,
);
sub name_and_title {
my ($self) = @_;
my $name = $self->name;
my $title = $self->title;
return "$name, $title";
}
1; # Magic true value.
}
package Employee::Former {
use strict;
use warnings;
use Moose;
use namespace::autoclean; # Code this instead of coding "no Moose;" a
+t the end of the package block.
extends 'Employee';
override name_and_title => sub {
my ($self) = @_;
my $old = super;
return "$old (Former)";
};
# Slide #85.
# The + says "we're overriding the definition in our superclass. Ever
+ything stays the same
# except for the provided changes."
# Here, we give a default. If no value is given, the default is used,
+ which lets us satisfy the
# "required" even when no value was given in the call to the construct
+or.
has '+title' => (
default => 'Team Member',
);
1; # Magic true value.
}
# Commented out. use Employee;
my $peon = Employee->new({
name => 'William Toady',
title => 'Associate Assistant',
});
say $peon->name_and_title; # Get name_and_title.
# Commented out. use Employee::Former;
my $ex_peon = Employee::Former->new({
name => 'William Toady',
title => 'Associate Assistant',
});
say $ex_peon->name_and_title; # Get name_and_title.
# Where is the test code? Answer: Slide #87.
# Name this script mip-087-001.pl
my $ex_peon2 = Employee::Former->new({
name => 'William Toady',
});
say $ex_peon2->name_and_title; # ===> William Toady, Team Member (for
+mer)
__END__
Results:
William Toady, Associate Assistant
William Toady, Associate Assistant (Former)
William Toady, Team Member (Former)
I recently looked at Raku after the name change. (The Windows version is actually easy to install now.)
So, after reading much of the "Think Raku" book, I tried to write counterparts to my collection of Perl 5 RJBS presentation "Moose is Perl" programs.
Well,I am stuck. (I thought Moose was 1:1 Raku OOP. Boy, was I wrong.)
Raku program attempt: mip-087-001.raku
class Employee {
has Str $.name is readonly is required; # "readonly" is the defaul
+t.
has Str $.title is rw is required;
method name_and_title {
my $name = self.name;
my $title = self.title;
return "$name, $title";
}
}
class Employee::Former is Employee {
method name_and_title {
my $old = callsame;
return "$old (Former)";
}
# Slide #85.
# The + says "we're overriding the definition in our superclass. Ever
+ything stays the same
# except for the provided changes."
# Here, we give a default. If no value is given, the default is used,
+ which lets us satisfy the
# "required" even when no value was given in the call to the construct
+or.
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
# Is there not a Raku counterpart for this?
# has '+title' => (
# default => 'Team Member',
# );
# My poor attempt:
has $.title is default('Team Member');
# XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
}
# Commented out. use Employee;
my $peon = Employee.new(
name => 'William Toady',
title => 'Associate Assistant',
);
say $peon.name_and_title; # Get name_and_title.
# Commented out. use Employee::Former;
my $ex_peon = Employee::Former.new(
name => 'William Toady',
title => 'Associate Assistant',
);
say $ex_peon.name_and_title; # Get name_and_title.
# Where is the test code? Answer: Slide #87.
# Name this script mip-087-001.raku
my $ex_peon2 = Employee::Former.new(
name => 'William Toady',
);
say $ex_peon2.name_and_title; # ===> William Toady, Team Member (form
+er)
Results:
William Toady, Associate Assistant
William Toady, Associate Assistant (Former)
The attribute '$!title' is required, but you did not provide a value f
+or it.
in block <unit> at C:\2020-Raku-001\Raku-scripts\mip-087-001.raku li
+ne 60