deppp (owner)

Revisions

gist: 225461 Download_button fork
public
Public Clone URL: git://gist.github.com/225461.git
Embed All Files: show embed
moosex-getopt.pl #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package App;
use Moose;
use Moose::Util::TypeConstraints;
 
with 'MooseX::Getopt';
    
use URI;
 
subtype 'Uri'
    => as 'Object'
    => where { $_ =~ m{(?:http://)?(?:www\.)?[^.]*\.com/?} }
    => message { "only .com domains are accepted, no subdomains (except www)" };
    
coerce 'Uri'
    => from 'Str'
    => via { URI->new($_) };
 
# we need to create a custom mapping, so Getopt knows what is Uri,
# in this case it's a simple string
MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
    'Uri' => '=s'
);
 
has 'url' => ( isa => 'Uri', is => 'rw', coerce => 1, required => 1 );
 
package main;
use Data::Dump 'dump';
 
my $app = App->new_with_options;
dump $app->url;