Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Perlコーディングテクニック2018

akiym
March 03, 2018

 Perlコーディングテクニック2018

YAPC::Okinawa 2018 ONNASON

akiym

March 03, 2018
Tweet

More Decks by akiym

Other Decks in Programming

Transcript

  1. VTFTUSJDUVSFT use strict; use warnings FATAL => 'all'; use warnings

    NONFATAL => qw( exec recursion internal malloc newline experimental deprecated portable ); no warnings 'once';
  2. VTFTUSJDUVSFT use strict; use warnings FATAL => 'all'; use warnings

    NONFATAL => qw( exec recursion internal malloc newline experimental deprecated portable ); no warnings 'once'; {a => 1, 2} Odd number of elements in anonymous hash
  3. sub new { my ($class, %args) = @_; croak("required: z")

    unless exists $args{z}; return bless \%args, $class; } sub z { $_[0]->{z} } CMFTT
  4. use Moose; extends 'Point'; has 'z' => (isa => 'Int',

    is => 'rw', required => 1); after 'clear' => sub { my $self = shift; $self->z(0); }; .PPTF.PVTF.PP
  5. use Class::Tiny qw(x y z); sub BUILD { my ($self,

    $args) = @_; croak("required: z") unless exists $args->{z}; } $MBTT5JOZ
  6. use Class::Accessor::Lite ( ro => [qw/x y z/], ); sub

    new { my ($class, %args) = @_; croak("required: z") unless exists $args{z}; return bless \%args, $class; } CMFTT  $MBTT"DDFTTPS-JUF
  7. .PEVMF'VODUJPOT use Module::Functions qw/get_public_functions/; use Exporter 'import'; our @EXPORT =

    get_public_functions; use JSON qw/decode_json/; sub bar { ... } sub _foo { ... } 㢩鿇ַ׵FYQPSU׃׋ꟼ侧כ搀鋔 @ַ׵㨣ת׷ꟼ侧כ搀鋔
  8. *NQPSUFS use Importer Importer => 'import'; use Importer 'List::Util' =>

    ( any => {-as => 'list_util_any'}, ); VTF&YQPSUFSJNQPSUךַ׻׶ ⴽせדJNQPSUדֹ׷
  9. sub create_xxx { my (%args) = @_; if ($args{foo}) {

    ... } db->insert( x => 100, y => 'abc', %args, ); } ְ׹׿זךָ床ׇ׷ 剑穄涸חכ湫䱸床׃ג׃תֲ ㄎן⳿׃⩎חׅץג㨻י׷
  10. use Params::Validate qw(:all); sub foo { my %args = validate(@_,

    { foo => { type => ARRAYREF }, bar => { isa => 'Bar' }, }); } 1BSBNT7BMJEBUF
  11. use Data::Validator; sub foo { state $rule = Data::Validator->new( foo

    => { isa => 'ArrayRef' }, bar => { isa => 'Bar' }, ); my $args = $rule->validate(@_); } %BUB7BMJEBUPS .PVTFך㘗 غٔر٦ةך㹀纏ךٍؗحءָُ〳腉 「ֽ《׏׋ػًٓ٦ةכIBTISFGד乼⡲
  12. use Smart::Args; sub foo { args my $foo => 'ArrayRef',

    my $bar => 'Bar', ; } 4NBSU"SHT !@׾床ׅ䗳銲ָזְ غٔر٦ةך㹀纏׾ٍؗحءُדֹזְ 「ֽ《׏׋ػًٓ٦ةכ㢌侧ד乼⡲
  13. Any Item Bool Maybe[`a] Undef Defined Value Str Num Int

    ClassName RoleName Ref ScalarRef ArrayRef[`a] HashRef[`a] CodeRef RegexpRef GlobRef FileHandle Object ArrayRef[User] Maybe[Str] .PPTF✼䳔
  14. Map[`k, `v] Tuple[...] Dict[...] Optional[`a] InstanceOf[`a] ConsumerOf[`a] HasMethods[`a] Overload[`a] Tied[`a]

    StrMatch[`a] Enum[`a] OptList LaxNum StrictNum CycleTuple[`a] MkOpt Split[`a] Join[`a] Dict[a => Int, b => Str] Enum['public', 'private'] .PPTF95ZQFT4USVDUVSFE✼䳔
  15. use Mouse::Util::TypeConstraints; subtype 'NonEmptyStr', as 'Str', where { length($_) >

    0 }; my $type = find_type_constraint('NonEmptyStr'); $type->check('str'); .PVTF ؚٗ٦غٕז㘗׾㹑鎉
  16. use Type::Standard -types; use Type::Utils -all; subtype 'NonEmptyStr', as Str,

    where { length($_) > 0 }; NonEmptyStr->check('str'); 5ZQF5JOZ ׉ך㜥ד㘗ךؔـآؙؑز׾⡲䧭 㢩鿇ַ׵⢪ֲ㜥さכFYQPSUׇׁ׷
  17. use Types::Standard -types; sub foo { args my $x =>

    {isa => 'Int', default => 100}, my $y => {isa => Str, default => 'abc'}, my $bar => 'Bar', my $bar => 'NonEmptyStr', ; } 5ZQF5JOZך㘗׮⢪ִ׷ 俑㶵⴨ה׃ג׮床ׇ׷ .PVTF6UJM5ZQF$POTUSBJOUTד㹑鎉׃׋㘗׮⢪ִ׷
  18. use Types::Standard qw/Str/; use Params::ValidationCompiler; sub foo { state $validator

    = validation_for( params => { foo => { type => Str }, }, ); my %args = $validator->(@_); } 1BSBNT7BMJEBUJPO$PNQJMFS
  19. my $x = {a => [1, 2, 3], b =>

    [4]}; 兛鸐ךEFSFGFSFODF @{$x->{a}} $x->{a}->@* QPTUYEFSFGFSFODF
  20. map { $_->id } @{ Foo->bar( ... ) }; map

    { $_->id } Foo->bar( ... )->@*; 兛鸐ךEFSFGFSFODF QPTUYEFSFGFSFODF
  21. use constant A => [1, 2, 3]; !"דכזְ׋׭" ד ㄎן⳿׃גֶֻ䗳銲ָ֮׷

    兛鸐ךEFSFGFSFODF @{A()} A->@* QPTUYEFSFGFSFODF @{+A}