From 170b7d4ea73dac6bfbde4813a325f73e11ea5360 Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 13 Jul 2018 12:09:51 +0100 Subject: style --- lib/Getopt/Dakkar.pm | 5 +-- lib/Getopt/Dakkar/Style.pm | 105 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 lib/Getopt/Dakkar/Style.pm (limited to 'lib/Getopt') diff --git a/lib/Getopt/Dakkar.pm b/lib/Getopt/Dakkar.pm index c3b9444..d8edd0c 100644 --- a/lib/Getopt/Dakkar.pm +++ b/lib/Getopt/Dakkar.pm @@ -1,8 +1,5 @@ package Getopt::Dakkar; -use strict; -use warnings; -use 5.026; -use experimental qw(signatures postderef); +use Getopt::Dakkar::Style; # VERSION # ABSTRACT: the best command line parser ever diff --git a/lib/Getopt/Dakkar/Style.pm b/lib/Getopt/Dakkar/Style.pm new file mode 100644 index 0000000..e843262 --- /dev/null +++ b/lib/Getopt/Dakkar/Style.pm @@ -0,0 +1,105 @@ +package Getopt::Dakkar::Style; +use 5.026; +use strictures 2; +use true; +use Import::Into; +use Try::Tiny (); +use Carp (); +use feature (); +use experimental (); +use Path::Tiny (); + +# VERSION +# ABSTRACT: simplify writing things in the right style + +sub use_types { + my ($caller) = @_; + require Types::Standard; + require Type::Params; + + Types::Standard->import::into( + $caller, + "Any", + "Item", + "Bool", + "Undef", + "Defined", + "Value", + "Str", + "Num", + "Int", + "ClassName", + "RoleName", + "Ref", + "CodeRef", + "RegexpRef", + "FileHandle", + "ArrayRef", + "HashRef", + "ScalarRef", + "Object", + "Maybe", + "Map", + "Optional", + "Tuple", + "Dict", + "InstanceOf", + "ConsumerOf", + "HasMethods", + ); + + Type::Params->import::into($caller); +} + +sub import { + my $class = shift; + my (@args) = @_; + my $caller = caller(); + + strict->import(); + feature->import(':5.26'); + Try::Tiny->import::into($caller); + Carp->import::into($caller); + feature->import::into($caller, ':5.26'); + true->import({ into => $caller }); + + my %arg = map { $_ => 1 } @args; + if ($arg{class}) { + require Moo; + Moo->import::into($caller); + $arg{types}=1; + } + if ($arg{role}) { + require Moo::Role; + Moo::Role->import::into($caller); + $arg{types}=1; + } + if ($arg{types}) { + use_types($caller); + } + if ($arg{test}) { + require Test::Most; + require Test::Warnings; + require lib; + Test::Most->import::into($caller); + Test::Warnings->import::into($caller); + lib->import::into( + $caller, + Path::Tiny::path(__FILE__)->parent(4)->child('t/lib')->stringify, + ); + } + + # This must come after anything else that might change warning + # levels in the caller (e.g. Moo) + strictures->import::into($caller,version=>2); + # and then we disable warnings for the experimental features we + # want + experimental->import::into($caller,qw(postderef signatures)); + + # Auto-clean up imported symbols + namespace::autoclean->import( + -cleanee => $caller, + ); + + return; +} -- cgit v1.2.3