PrePAN

Sign in to PrePAN

Modo An attempt at a Modern Perl 5 implementation

Good

Synopsis

# enables use warnings and strict for you
use Modo;

# Modo now uses autobox. Which means you can create strings and arrays like so
my $str = "Hello, World"->ob;
my $arr = ['hello', 'there']->ob;

# Basic Str example
my $str = Str->new("Hello, World!");
$str->say; # prints Hello, World!

# What you can do with it after
$str->concat("How")->concat("Are")->concat->("You?")->say;

# or

$str + "How" + "Are" + "You?";
    
say $str->first; # get the first character
say $str->substr(0, 1); # sure, or use substr

# Splitting a string with the division operator returns an Array class

my $str = Str->new("Hello:there:world");
($str / ':')->loop(sub {
    say $_;
});

# Hello
# there
# world

## Ints

my $int = Int->new(5);
say $int->val; # prints 5
        
# chain methods to perform math
say $int->add(5)->divide(2)->subtract(3)->mult(7);

# or

$int + 5 / 2 - 3 * 7;

## enums

use Modo;

enum Bool => ( 'True:1', 'False:0' );

# set up a couple of test methods to test against our new Boolean type
sub ok { return 1; }
sub not_ok { return 0; }

if (ok() == Bool->True) { say "We're good!"; }
if (not_ok() == Bool->False) { say "This is false"; }

## Class building

{
    package MyFoo;
    use Modo as => 'Class';
    
     has 'x' => ( is => 'rw', default => 5 );

     private 'secret' => sub {
         say "Private method can't be called from outside of this class";
     };

}

my $foo = MyFoo->new;
say $foo->x; # prints 5
    
$foo->x(7); # updates x to 7
say $foo->x; # prints 7

## Arrays

my $day = 'Tuesday';
my $weekdays = Array->new(qw< Monday Tuesday Wednesday Thursday Friday >);
    
if ($weekdays->any($day)) {
    say "OK, I'll see you on $day!";
}

# or

if ($weekdays ~~ $day) { .. }

$weekdays->loop(sub {
    say $_;
});

# push new arrays
my @a = qw(Saturday);
$weekdays + \@a;

# do it all inline
($weekdays + \@a)->loop(sub { say $_; });

# insert to the beginning of an Array
$weekdays << ['Sunday'];

Description

Modo is an attempt at implementing a Modern feel to perl5. Obviously this is my interpretation and yours will probably vary. It's still in its infancy at the moment, but as you can see I've borrowed a bit from perl6 and turned it into perl5. There is no source filtering or Devel::Declare stuff going on to change the syntax - it's pure perl5 and will remain that way. Yes, I know the pod sucks. Documentation isn't my best of traits.. sorry.

Comments

It looks like your "modern feel" is "everything is an object" and "everything must be created with new". I disagree with the latest especially as modules such as [autobox](https://metacpan.org/module/autobox) exist.
I'd love to be able to add autobox into Modo, so you don't need to call ->new every time. But the overloaded operators rely on instances like that, so I can't just throw autobox into the mould and have it all work unfortunately. For example:

use autobox SCALAR => 'Str';
use Modo;

say "Hey" + " there"; # complains that it's not numeric.. but Modo's overloaded operators allows you to do this

If I do this, it works

say "Hey"->new("Hey") + " there"; # but damn that looks ugly

If there was a way to create the object properly with autobox without the need for ->new I would happily use it
I can minimize it, and create a method called 'ob' that blesses it. So you can call my $str = "Hello"->ob instead of Str->new("Hello").
If you know of a better way I'm open to suggestions.
Perl has it right when it separates operators for strings (.) and for numerics (+). What you are trying to do with this overloading is to make perl code look like inferior languages. I don't see how it is "modern feel".
Note that Perl 6 still has separate operators for strings (~) and numerics (+).
Also note that to implement { my $str = "Hello"->ob } you will have to inject the "ob" method in the UNIVERSAL package. I would not call that clean either.
"Modern" generally means a change from traditional styles or values. If I was to keep everything the same, such as the concatenation style, then how would that be perceived as 'Modern'?
If you look at the github code, it's not touching UNIVERSAL at all. If you have a problem with it, don't use it. Modo isn't for everyone, but it does have a following of people who are interested to see where it is going.
If the plague swept the world again, that would be a change, but it wouldn't represent a modernisation of the world, nor would it be an improvement.

Please sign up to post a review.