#!/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.html/ 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;" at 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;" at 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. Everything 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 constructor. 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 (former) __END__ #### William Toady, Associate Assistant William Toady, Associate Assistant (Former) William Toady, Team Member (Former) #### class Employee { has Str $.name is readonly is required; # "readonly" is the default. 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. Everything 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 constructor. # 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 (former) #### William Toady, Associate Assistant William Toady, Associate Assistant (Former) The attribute '$!title' is required, but you did not provide a value for it. in block at C:\2020-Raku-001\Raku-scripts\mip-087-001.raku line 60