Trying Object::Pad

TL;DR

I gave Object::Pad a try.

I’m intrigued by the ongoing initiative to Bring Modern OO to the Core of Perl, so I decided to give Object::Pad a try.

My first (stripped down) example class was this:

use Object::Pad;

class TestClass {
   has $this :param :reader = 'that';
   method greet { say 'Hello, World!' }
   method announce ($msg) { say $msg }
}

and I hit an error! This error (line numbers will make sense shortly):

syntax error at lib/TestClass.pm line 9, near "method announce ("
Global symbol "$msg" requires explicit package name (did you forget to declare "my $msg"?) at lib/TestClass.pm line 9.
syntax error at lib/TestClass.pm line 9, at EOF
Parse error at lib/TestClass.pm line 9.
Expected } at lib/TestClass.pm line 9.

So I was… What The Hell?!?

Let’s zoom out a bit to the whole program…

package TestClass;
use v5.24;
use warnings;
use Object::Pad;

class TestClass {
   has $this :param :reader = 'that';
   method greet { say 'Hello, World!' }
   method announce ($msg) { say $msg }
}

exit sub {
   TestClass->new->announce('Hey all!');
}->(@ARGV) unless caller;

1;

There it is… the culprit! I’m using Perl in version 5.24, which is not supported for the signatures syntax for method, according to the author.

So… either I update my perl, or I stick with the old-fashioned way of taking parameters, i.e. use @_:

package TestClass;
use v5.24;
use warnings;
use Object::Pad;

class TestClass {
   has $this :param :reader = 'that';
   method greet { say 'Hello, World!' }
   method announce { say $_[0] }
}

exit sub {
   TestClass->new->announce('Hey all!');
}->(@ARGV) unless caller;

1;

Now this works and does not complain:

$ perl TestClass.pm 
Hey all!

What luck… I hit my first bug from the very beginning 😂


Comments? Octodon, , GitHub, Reddit, or drop me a line!