Commit | Line | Data |
d9d1529d |
1 | |
2 | package Foo; |
3 | use Moose; |
4 | use MooseX::Params::Validate; |
5 | use overload ( |
6 | qw{""} => 'to_string', |
7 | ); |
8 | |
9 | has 'id' => ( is => 'ro', isa => 'Str', default => '1.10.100' ); |
10 | |
11 | sub to_string { |
12 | my ($self, %args) = validated_hash( \@_, |
13 | padded => { isa => 'Bool', optional => 1, default => 0 }, |
14 | ); |
15 | |
16 | # 1.10.100 => 0001.0010.0100 |
17 | my $id = $args{ padded } |
18 | ? join( '.', map { sprintf( "%04d", $_ ) } split( /\./, $self->id ) ) |
19 | : $self->id; |
20 | |
21 | return $id; |
22 | } |
23 | |
24 | package main; |
25 | use Test::More tests => 4; |
26 | use strict; |
27 | use warnings; |
28 | |
29 | isa_ok( my $foo = Foo->new(), 'Foo', 'new' ); |
30 | |
31 | is( $foo->id, '1.10.100', 'id' ); |
32 | |
33 | is( $foo->to_string, '1.10.100', 'to_string' ); |
34 | |
35 | is( $foo->to_string( padded => 1 ), '0001.0010.0100', 'to_string( padded => 1 )' ); |
36 | |