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. 1FSM؝٦ر؍ؚٝ

    ذؙصحؙ
    BLJZN
    :"1$0LJOBXB0//"40/

    View full-size slide

  2. 1FSMחꟼׅ׷ְ׹׿ז鑧׾
    ׃תׅ

    View full-size slide

  3. 剑鵚ך1FSM歲꥔

    View full-size slide

  4. 剑鵚ֲֿ׃גְ׷הְֲ鑧׾
    耀ֹ׋ְ

    View full-size slide

  5. 剑鵚ךٌآُ٦ٕ׾
    ס׶ִַ׷

    View full-size slide

  6. VTFTUSJDU
    VTFXBSOJOHT
    VTFVUG

    View full-size slide

  7. VTFW
    VTFXBSOJOHT
    VTFVUG
    use strict;
    use feature ':5.26';

    View full-size slide

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

    View full-size slide

  9. 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

    View full-size slide

  10. ؙٓأؽٕت

    View full-size slide

  11. sub new {
    my ($class, %args) = @_;
    croak("required: z") unless exists $args{z};
    return bless \%args, $class;
    }
    sub z { $_[0]->{z} }
    CMFTT

    View full-size slide

  12. Ⰻ鿇䩛דװ׷崢

    View full-size slide

  13. use Moose;
    extends 'Point';
    has 'z' => (isa => 'Int', is => 'rw', required => 1);
    after 'clear' => sub {
    my $self = shift;
    $self->z(0);
    };
    .PPTF.PVTF.PP

    View full-size slide

  14. 堣腉搀꼽ח⢪ֲֶה׃ג
    醱꧟חז׶ָ׍

    View full-size slide

  15. use Class::Tiny qw(x y z);
    sub BUILD {
    my ($self, $args) = @_;
    croak("required: z") unless exists $args->{z};
    }
    $MBTT5JOZ

    View full-size slide

  16. 㼭ׁז׮ך׾
    穈׫さ׻ׇג⢪ֲ

    View full-size slide

  17. 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

    View full-size slide

  18. 鵚➿涸
    FYQPSU遭

    View full-size slide

  19. use Exporter 'import';
    our @EXPORT = qw/foo bar baz/;
    Foo.pm
    test.pl
    use Foo;
    foo();

    View full-size slide

  20. 倜׃ְꟼ侧ך鷄⸇䖓ח
    !&91035ח׮鷄⸇ׅ׷ך׾
    䘌׸ָ׍

    View full-size slide

  21. .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׃׋ꟼ侧כ搀鋔
    @ַ׵㨣ת׷ꟼ侧כ搀鋔

    View full-size slide

  22. *NQPSUFS
    use Importer Importer => 'import';
    use Importer 'List::Util' => (
    any => {-as => 'list_util_any'},
    );
    VTF&YQPSUFSJNQPSUךַ׻׶
    ⴽせדJNQPSUדֹ׷

    View full-size slide

  23. ٌآُ٦ٕ
    ٗ٦ت٦

    View full-size slide

  24. ⹛涸חٌآُ٦ٕ׾铣׫鴥׬
    ❌ eval qq{require "Plugin/$name.pm"}

    View full-size slide

  25. 6/*7&34"-SFRVJSF
    'Foo::Bar'->require;דֹ׷
    ַ׏ְְֿ׌ֽ*/$״׶ٗ٦س幥׫ַⴻ㹀

    View full-size slide

  26. .PEVMF-PBE
    ؝،ٌآُ٦ٕ
    ءٝفٕ׌׃暴ח⢪׏ג㉏겗זְ

    View full-size slide

  27. $MBTT-PBE
    USZ@MPBE@DMBTT
    ׅדחٗ٦س幥׫ַⴻ倖ׅ׷JT@DMBTT@MPBEFE

    View full-size slide

  28. .PEVMF3VOUJNF
    $MBTT-PBE׮ⰻ鿇ד⢪׏גְ׷
    ٌآُ٦ٕٗ٦ت٦ך㹀殢

    View full-size slide

  29. require_module($name);
    use_package_optimistically($name);
    compose_module_name('Foo', $name);
    ٌآُ٦ٕⰻדؒٓ٦ָ涪欰׃׋הֹ׌ֽ⢽㢩׾䫎־׷
    湫䱸KPJO 'PP OBNF
    ׃זְ

    View full-size slide

  30. ֮׷ط٦يأل٦أ⟃♴ך
    ٌآُ٦ٕ׾ׅץגٗ٦سׅ׷

    View full-size slide

  31. use Module::Find qw/useall/;
    useall('Foo::Handler');
    .PEVMF'JOE
    'PP)BOEMFS⟃♴ח֮׷
    'PP)BOEMFS"װ'PP)BOEMFS"#׾ٗ٦س

    View full-size slide

  32. 5ZQF5JOZ׾
    ⢪׏ג㘗׾⢪ֲ

    View full-size slide

  33. ؝٦سָ竰ֺ駈ׁ׸גְ׏׋
    ذأزךقٕػ

    View full-size slide

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

    View full-size slide

  35. UZQPח䓲ְ
    ⢪ְ倯ָⴓַ׵זְךד
    ְ׹ְ׹鋅חְֻ

    View full-size slide

  36. 床׏גֹ׋䒷侧׾
    ثؑحؙׅ׷

    View full-size slide

  37. ֿ׿זהֹחכغٔر٦ة
    䗳갭ػًٓ٦ةָ床ׁ׸גְ׷ַ
    㘗ثؑحؙ

    View full-size slide

  38. use Params::Validate qw(:all);
    sub foo {
    my %args = validate(@_, {
    foo => { type => ARRAYREF },
    bar => { isa => 'Bar' },
    });
    }
    1BSBNT7BMJEBUF

    View full-size slide

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

    View full-size slide

  40. use Smart::Args;
    sub foo {
    args my $foo => 'ArrayRef',
    my $bar => 'Bar',
    ;
    }
    4NBSU"SHT
    !@׾床ׅ䗳銲ָזְ
    غٔر٦ةך㹀纏׾ٍؗحءُדֹזְ
    「ֽ《׏׋ػًٓ٦ةכ㢌侧ד乼⡲

    View full-size slide

  41. !@׾床ׁ׆חㄎן⳿׃⯋ך!@׾《׸׷
    1BE8BMLFS׾⢪ְծ床׃׋㢌侧せ׾《׶⳿ׅ

    View full-size slide

  42. use Mouse::Util::TypeConstraints;
    my $type =
    find_or_create_isa_type_constraint('Int');
    $type->check($x);
    ⰻ鿇דכ.PVTFך㘗׾䪔ֲ

    View full-size slide

  43. 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✼䳔

    View full-size slide

  44. 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✼䳔

    View full-size slide

  45. .PVTFה5ZQF5JOZך麩ְ

    View full-size slide

  46. use Mouse::Util::TypeConstraints;
    subtype 'NonEmptyStr',
    as 'Str',
    where { length($_) > 0 };
    my $type = find_type_constraint('NonEmptyStr');
    $type->check('str');
    .PVTF
    ؚٗ٦غٕז㘗׾㹑鎉

    View full-size slide

  47. use Type::Standard -types;
    use Type::Utils -all;
    subtype 'NonEmptyStr',
    as Str,
    where { length($_) > 0 };
    NonEmptyStr->check('str');
    5ZQF5JOZ
    ׉ך㜥ד㘗ךؔـآؙؑز׾⡲䧭
    㢩鿇ַ׵⢪ֲ㜥さכFYQPSUׇׁ׷

    View full-size slide

  48. 4NBSU"SHT5ZQF5JOZ
    4NBSU"SHTד5ZQF5JOZך㘗׾
    䪔ִ׷״ֲח׃׋׮ך
    4NBSU"SHTה㢌׻׵׆⢪ִ׷

    View full-size slide

  49. 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ד㹑鎉׃׋㘗׮⢪ִ׷

    View full-size slide

  50. use Types::Standard qw/Str/;
    use Params::ValidationCompiler;
    sub foo {
    state $validator = validation_for(
    params => {
    foo => { type => Str },
    },
    );
    my %args = $validator->(@_);
    }
    1BSBNT7BMJEBUJPO$PNQJMFS

    View full-size slide

  51. 倜׃ְ1FSMך
    堣腉׾⢪ֲ

    View full-size slide

  52. QPTUYEFSFGFSFODF
    QFSM⟃꣬ד姻䒭堣腉⻉

    View full-size slide

  53. $$a $a->$*
    @$a $a->@*
    $#$a $a->$#*
    %$a $a->%*
    &$a $a->&*
    *$a $a->**

    View full-size slide

  54. $$a $a->$*
    @$a $a->@*
    $#$a $a->$#*
    %$a $a->%*
    &$a $a->&*
    *$a $a->**

    View full-size slide

  55. my $x = {a => [1, 2, 3], b => [4]};
    兛鸐ךEFSFGFSFODF
    @{$x->{a}} $x->{a}->@*
    QPTUYEFSFGFSFODF

    View full-size slide

  56. map { $_->id } @{ Foo->bar(
    ...
    ) };
    map { $_->id } Foo->bar(
    ...
    )->@*;
    兛鸐ךEFSFGFSFODF
    QPTUYEFSFGFSFODF

    View full-size slide

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

    View full-size slide

  58. QFSM
    ׉׸⟃⵸ד׮⢪ְ׋ְ

    View full-size slide

  59. QPTUYEFSFGFSFODF׾㢌䳔
    IUUQTHJUIVCDPNBLJZNMFHBDZ@QPTUYEFSFG

    View full-size slide

  60. 11*׾⢪׏ג
    WBS!׾!WBSח
    剅ֹ䳔ִ׷

    View full-size slide

  61. ➙ּׅQPTUYEFSFGFSFODF
    ⢪ִ׷

    View full-size slide

  62. 5.5085%*
    5IFSFT.PSF5IBO0OF8BZ5P%P*U

    View full-size slide