| 1 | #!/usr/bin/perl -c | ||
| 2 | |||
| 3 | package MooseX::Types::OpenModeStr; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | MooseX::Types::OpenModeStr - Type for open mode string | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | package My::Class; | ||
| 12 | use Moose; | ||
| 13 | use MooseX::Types::OpenModeStr; | ||
| 14 | has file => ( isa => 'Str' ); | ||
| 15 | has mode => ( isa => 'OpenModeStr' ); | ||
| 16 | |||
| 17 | package main; | ||
| 18 | my $fin = My::Class->new( file => '/etc/passwd', mode => 'r' ); | ||
| 19 | my $fout = My::Class->new( file => '/tmp/pwdnew', mode => 'w' ); | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This module provides Moose type which represents open mode string. It can be | ||
| 24 | Perl-style canonical mode string (i.e. "+>") or C-style mode string (i.e. | ||
| 25 | "w+"). | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | |||
| 30 | use strict; | ||
| 31 | use warnings; | ||
| 32 | |||
| 33 | our $VERSION = '0.1004'; | ||
| 34 | |||
| 35 | use Moose::Util::TypeConstraints; | ||
| 36 | |||
| 37 | |||
| 38 | subtype OpenModeStr => ( | ||
| 39 | as 'Str', | ||
| 40 | where { /^([rwa]\+?|\+?(<|>>?))$/ }, | ||
| 41 | optimize_as { | ||
| 42 | defined $_[0] && !ref($_[0]) | ||
| 43 | && $_[0] =~ /^([rwa]\+?|\+?(<|>>?))$/ | ||
| 44 | }, | ||
| 45 | ); | ||
| 46 | |||
| 47 | |||
| 48 | 1; | ||
| 49 | |||
| 50 | |||
| 51 | __END__ |