Skip to content

Instantly share code, notes, and snippets.

@deppp
Created November 3, 2009 21:25
Show Gist options
  • Save deppp/225461 to your computer and use it in GitHub Desktop.
Save deppp/225461 to your computer and use it in GitHub Desktop.
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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment