Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from Moo to built-in class #685

Merged
merged 8 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions .github/workflows/exercises.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ jobs:
strategy:
matrix:
os: ['ubuntu-latest', 'macos-latest']
perl: ['5.40', '5.38', '5.36', '5.34', '5.32', '5.30', '5.28', '5.26', '5.24', '5.22', '5.20']
perl: ['5.40', '5.38', '5.36', '5.34', '5.32', '5.30', '5.28', '5.26']
distribution: ['default']
include:
- os: 'windows-latest'
perl: '5.38'
perl: '5.40'
distribution: strawberry

name: Perl ${{ matrix.perl }} on ${{ matrix.os }}
Expand All @@ -22,22 +22,12 @@ jobs:
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332

- name: Set up perl
uses: shogo82148/actions-setup-perl@87c3ad5232e6c1b59da33bfde4fb24b0482901f1
uses: shogo82148/actions-setup-perl@9c1eca9952ccc07f9ca4a2097b63df93d9d138e9
with:
perl-version: ${{ matrix.perl }}
distribution: ${{ matrix.distribution }}

- run: cpm install -g Test2::V0 UUID

- name: Exercise modules
shell: pwsh
run: >
(Get-ChildItem ./exercises/practice/*/.meta/solutions -Recurse -Include cpanfile).ForEach({
cd (Split-Path -Path $_ -Parent);
echo "$_";
cpm install;
cd -;
})
- run: cpm install -g Test2::V0 UUID Feature::Compat::Class

- name: Test with prove
run: prove exercises/practice/*/.meta/solutions/ --recurse --jobs 2
Expand Down
1 change: 1 addition & 0 deletions .perltidyrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
--novalign-side-comments
--freeze-newlines
--keep-old-breakpoints-after='('
--nowant-right-space='A'
4 changes: 2 additions & 2 deletions cpanfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
requires 'lib::gitroot';
requires 'Data::Dmp';
requires 'Moo';
requires 'Feature::Compat::Class';
requires 'lib::gitroot';
requires 'namespace::autoclean';
requires 'Path::Tiny';
requires 'Perl::Tidy';
Expand Down
22 changes: 12 additions & 10 deletions docs/INSTALLATION.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Installation

## Perl Version Notes
The track targets the latest stable release of Perl, but it is possible to use versions of Perl as old as 5.20.
The track targets the latest stable release of Perl.
It is possible to use older versions of Perl, but it may be necessary to make adjustments to the provided stub files for compatibility.
If you are unable to use the version specified in the stub files, it is recomended to replace `use v5.XX;` with the following code:

```perl
Expand All @@ -10,6 +11,8 @@ use warnings;
use feature qw<signatures>;
```

If the stub file contains `use experimental qw<class>;` and the version of Perl you are using does not support this, you may either substitute this with `use Feature::Compat::Class;`, or use an alternative Object Orientation system, e.g., [Moo](https://metacpan.org/pod/Moo).

## Unix/Linux/Mac OSX
Perl is likely already installed, however it is possible that this may not be the latest version.
Run `perl -v` to check which version you have.
Expand Down Expand Up @@ -37,22 +40,23 @@ You can either install this directly from the site or use the [chocolatey packag

### Installing Necessary Modules
If you are using a version of Perl older than 5.40, you will need to install the `Test2::V0` testing bundle.
You will also need to install either `Feature::Compat::Class` or an alternative Object Orientation system for OOP exercises.
Use the following instructions to set this up (or feel free to use your favorite module installer if you are already familiar with Perl):

#### local::lib
* Ensure that the exercism CLI has already been installed and configured.
Running `exercism workspace` should show you the appropriate path.

* Download the `cpm` module installer and use it to install itself,
`local::lib`, and `Test2::V0` into your `exercism/perl5` directory:
`local::lib`, `Test2::V0`, and `Feature::Compat::Class` into your `exercism/perl5` directory:
```
# *nix
curl -sL --compressed https://git.io/cpm | \
perl - install -L $(exercism workspace)/perl5 App::cpm local::lib Test2::V0
perl - install -L $(exercism workspace)/perl5 App::cpm local::lib Test2::V0 Feature::Compat::Class

# Microsoft PowerShell
(Invoke-WebRequest -UseBasicParsing 'https://git.io/cpm').Content |
perl - install -L "$(exercism workspace)/perl5" App::cpm local::lib Test2::V0
perl - install -L "$(exercism workspace)/perl5" App::cpm local::lib Test2::V0 Feature::Compat::Class
```

* Run `local::lib` to set up your environment to use the modules you have
Expand All @@ -66,16 +70,14 @@ perl -I"$(exercism workspace)/perl5/lib/perl5"
-M"local::lib=--shelltype=powershell,$(exercism workspace)/perl5" | Invoke-Expression
```

The environment changes from the above command will not persist, and will
need to be rerun if you restart your shell. If you would like your environment
to be set up automatically each time, modify your `.bashrc` (or \*nix equivalent)
or your `PowerShell_profile.ps1` to include the above command.
The environment changes from the above command will not persist, and will need to be rerun if you restart your shell.
If you would like your environment to be set up automatically each time, modify your `.bashrc` (or \*nix equivalent) or your `PowerShell_profile.ps1` to include the above command.

The documentation for local::lib can be found [here](https://perldoc.pl/local::lib).

#### OS Packages
Alternatively, the package manager for your OS may have a Test2 Suite package
available for you to install. Available options include:
Alternatively, the package manager for your OS may have a Test2 Suite package available for you to install.
Available options include:

* Ubuntu: [libtest2-suite-perl](https://launchpad.net/ubuntu/+source/libtest2-suite-perl)
* Debian: [libtest2-suite-perl (via backports)](https://packages.debian.org/stretch-backports/libtest2-suite-perl)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,46 +1,17 @@
package BinarySearchTree;

use Moo;
use strict;
use warnings;
use experimental qw<signatures postderef postderef_qq>;
use Feature::Compat::Class;

package BinarySearchTree::Node {
use Moo;
no warnings qw<experimental::signatures>;

has data => (
is => 'ro',
);
has [qw<left right>] => (
is => 'rw',
);

sub set ( $self, $data ) {
if ( $data > $self->data ) {
if ( $self->right ) {
$self->right->set($data);
}
else {
$self->right( BinarySearchTree::Node->new( data => $data ) );
}
}
elsif ( $self->left ) {
$self->left->set($data);
}
else {
$self->left( BinarySearchTree::Node->new( data => $data ) );
}
}
};
class BinarySearchTree;

has root => (
is => 'rw',
);
field $root :param;

sub add ( $self, $data ) {
$self->root->set($data);
method add ($data) {
$root->set($data);
}

sub sort ($self) {
method sort () {
my @sorted;
my $sub;
$sub = sub {
Expand All @@ -55,8 +26,31 @@ sub sort ($self) {
$sub->( $node->right );
}
};
$sub->( $self->root );
$sub->($root);
return [@sorted];
}

class BinarySearchTree::Node {
field $data :reader :param;
field $left :reader;
field $right :reader;

method set ($new_data) {
if ( $new_data > $data ) {
if ($right) {
$right->set($new_data);
}
else {
$right = BinarySearchTree::Node->new( data => $new_data );
}
}
elsif ($left) {
$left->set($new_data);
}
else {
$left = BinarySearchTree::Node->new( data => $new_data );
}
}
}

1;
92 changes: 39 additions & 53 deletions exercises/practice/binary-search-tree/.meta/template-data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,15 @@ properties:
);
END

moo: 1
class: true
example: |-
package BinarySearchTree::Node {
use Moo;
no warnings qw<experimental::signatures>;
field $root :param;

has data => (
is => 'ro',
);
has [qw<left right>] => (
is => 'rw',
);

sub set ($self, $data) {
if ($data > $self->data) {
if ($self->right) {
$self->right->set($data);
}
else {
$self->right(BinarySearchTree::Node->new(data => $data));
}
}
elsif ($self->left) {
$self->left->set($data);
}
else {
$self->left(BinarySearchTree::Node->new(data => $data));
}
}
};

has root => (
is => 'rw',
);

sub add ($self, $data) {
$self->root->set($data);
method add ($data) {
$root->set($data);
}

sub sort ($self) {
method sort () {
my @sorted;
my $sub;
$sub = sub {
Expand All @@ -68,30 +37,47 @@ example: |-
$sub->($node->right);
}
};
$sub->($self->root);
$sub->($root);
return [@sorted];
}

stub: |-
package BinarySearchTree::Node {
use Moo;
class BinarySearchTree::Node {
field $data :reader :param;
field $left :reader;
field $right :reader;

has data => (
is => 'ro',
);
has [qw<left right>] => (
is => 'rw',
);
};
method set ($new_data) {
if ($new_data > $data) {
if ($right) {
$right->set($new_data);
}
else {
$right = BinarySearchTree::Node->new(data => $new_data);
}
}
elsif ($left) {
$left->set($new_data);
}
else {
$left = BinarySearchTree::Node->new(data => $new_data);
}
}
}

has root => (
is => 'rw',
);

sub add ($self) {
# $self->root contains the initial node.
stub: |-
field $root :param;

method add () {
# $root contains the initial node.
}

sub sort ($self) {
method sort () {
return [];
}

class BinarySearchTree::Node {
field $data :reader :param;
field $left :reader;
field $right :reader;
}
5 changes: 0 additions & 5 deletions exercises/practice/binary-search-tree/cpanfile

This file was deleted.

31 changes: 12 additions & 19 deletions exercises/practice/binary-search-tree/lib/BinarySearchTree.pm
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
package BinarySearchTree;

use v5.40;
use Moo;

package BinarySearchTree::Node {
use Moo;
use experimental qw<class>;

has data => (
is => 'ro',
);
has [qw<left right>] => (
is => 'rw',
);
};
class BinarySearchTree;

has root => (
is => 'rw',
);
field $root :param;

sub add ($self) {
method add () {

# $self->root contains the initial node.
# $root contains the initial node.
}

sub sort ($self) {
method sort () {
return [];
}

class BinarySearchTree::Node {
field $data :reader :param;
field $left :reader;
field $right :reader;
}

1;
1 change: 0 additions & 1 deletion exercises/practice/clock/.meta/solutions/cpanfile

This file was deleted.

Loading