From: Florian Ragwitz Date: Sun, 5 Apr 2009 22:10:53 +0000 (+0200) Subject: Implement String::substr. X-Git-Tag: 0.16~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=51327b985f790ad6e3a7541f50885e30fbebf2f4;p=gitmo%2FMooseX-AttributeHelpers.git Implement String::substr. --- diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/String.pm b/lib/MooseX/AttributeHelpers/MethodProvider/String.pm index 86bf8bd..e8a5028 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/String.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/String.pm @@ -68,10 +68,33 @@ sub inc : method { } sub clear : method { - my ($attr, $reader, $writer ) = @_; + my ($attr, $reader, $writer) = @_; return sub { $writer->( $_[0], '' ) } } +sub substr : method { + my ($attr, $reader, $writer) = @_; + return sub { + my $self = shift; + my $v = $reader->($self); + + my $offset = defined $_[0] ? shift : 0; + my $length = defined $_[0] ? shift : CORE::length($v); + my $replacement = defined $_[0] ? shift : undef; + + my $ret; + if (defined $replacement) { + $ret = CORE::substr($v, $offset, $length, $replacement); + $writer->($self, $v); + } + else { + $ret = CORE::substr($v, $offset, $length); + } + + return $ret; + }; +} + 1; __END__