New release of Method::Signatures is on its way to CPAN. I'm in a sprint to get all the sizzle implemented before PPW.
This release features improved parsing of method attributes, thanks to stealing code from Florian Ragwitz's MooseX::Method::Signatures.
Some parameter traits have been implemented. "is alias" lets you work with the parameter as an alias, altering it in the caller's namespace. Previously this was only possible by manipulating @_ directly.
These two are equivalent.
sub strip_ws {
my $self = shift;
$_[0] =~ s/^\s+//;
$_[0] =~ s/\s+$//;
return;
}
method strip_ws($str is alias) {
$str =~ s/^\s+//;
$str =~ s/\s+$//;
return;
}
my $str = " support the right to arm bears ";
Class->strip_ws($str); # works in place on $str
method echo($msg is ro) {
return $msg;
}
# $x and $y are required. $z is optional and defaults to 0
method print_position($x, $y, $z=0) {
print "$x, $y, $z";
}
# $url is required. $user and $pass are optional.
method get($url, $user?, $pass?) {
...
}
# year is required. The rest are optional and have defaults.
# They are all readonly.
method date2epoch(:$year! is ro, :$month = 1 is ro, :$day = 1 is ro,
:$hour = 0 is ro, :$min = 0 is ro, :$sec = 0 is ro) {
...
}
# Jan 1st, 2008 00:00:00
my $epoch = Date->date2epoch( year => 2008 );
# One minute after midnight
my $epoch = Date->date2epoch( year => 2008, min => 1 );
# $text is positional, the rest are named and optional
method format($text, :$justify = "left", :$quality) {
...
}
# left justified
Class->format("mu");
# right justified, Livejournal quality poetry.
Class->format(<<'POETRY', justify => "right", quality => "LJ");
...
POETRY
sub date2epoch {
my $self = shift;
my %args = @_;
croak "date2epoch() missing required argument \$year"
unless exists $args{year};
Readonly my $year = $args{year};
Readonly my $month = $args{month} || 1;
Readonly my $day = $args{day} || 1;
Readonly my $hour = $args{hour} || 0;
Readonly my $min = $args{min} || 0;
Readonly my $sec = $args{sec} || 0;
...
}