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

はてなリモートインターン2021 Perlブートキャンプ 講義資料

Hatena
October 08, 2021

はてなリモートインターン2021 Perlブートキャンプ 講義資料

Hatena

October 08, 2021
Tweet

More Decks by Hatena

Other Decks in Technology

Transcript

  1. 舅䄕磆☭ ˝ JEUL[XULT悂很㼵 ˝ 䇗僖⪌炘 ˝ 哅ⳉ塷 ˝ ؜ؠّٙ ˝

    (JHB7JFXFS ˝ עיםرؕؓٛ٭磙▼ ˝ 뇧嫎סJ׼؆ל
  2. غ؞ْٖ٤عעQFSMEPDך䌕ׂ % perldoc Carton # Ϟδϡʔϧʢޙड़ʣͷυΩϡϝϯτ % perldoc -f print

    # ૊ΈࠐΈؔ਺͸ -f % perldoc -v !" # ૊ΈࠐΈม਺͸ -v % perldoc perl
  3. ˝ ٓةٖ٭ٜ package Example!"Greeter; use strict; use warnings; sub greet

    { my ($class, $what) = !#; print "hello, $what\n"; } 1;
  4. ˝ ْؕ٤تؠٛوع use strict; use warnings; use lib 'lib'; use

    Example!"Greeter; Example!"Greeter!#greet('world'); ˝ 㲔车 % perl -Ilib main.pl hello, world
  5. ت؜ٚ ˝ חס⡁ ˝ 丗㰄׵乃㰄⮛׵׌׬יت؜ٚ my $scalar1 = 'test'; my

    $scalar2 = 1000; my $scalar3 = \@array; # ϦϑΝϨϯεʢޙड़ʣ ˝ ׷狜乃㰄⮛עGBMTZם⡁כ׊י䣽؂׿׾ ˝ ׵GBMTZ
  6. 鿥⮛ס䳩⛼ $array[0]; # get $array[1] = 'hoge'; # set push

    @array, 'meow'; # ࠷ޙʹཁૉΛ௥Ճ my $v = shift @array; # ࠷ॳͷཁૉΛऔΓআ͍ͯฦ͢ my $length = scalar @array; # ௕͞ for my $e (@array) { # શཁૉϧʔϓ print $e; }
  7. 鿥⮛ס䳩⛼ my @doubles = map { $_ * 2 }

    @numbers; # ͢΂ͯΛ2ഒʹͨ͠഑ྻΛ࡞Δ my @over20 = grep { $_ > 20 } @numbers; # 20ΑΓେ͖ͳ਺͚ͩΛूΊͨ഑ྻΛ࡞Δ ˝ $_ע攐墤㚺丗 ˝ ׆ס㕙⺬ךע鿥⮛ס⺨釐碛ֿꯂמ⹦׽⭳׈׿י⪌זיׂ׾
  8. ـشبٖ ˝ ַ؂׹׾ظؔؠب٘ػٛ׷ُشو my %hash = ( perl !" 'larry',

    ruby !" 'matz', ); ˝ עGBUDPNNBכ⽛ף׿յ QFSMMBSSZ ע QFSM  MBSSZ כ⺱┉ ˝ ⺓鱨ע鿥⮛סכ׀כ⺱喋םٛتعדֿյל؆ם㚺丗מ劲硯׌׾ ־ךؓؠجتס☼亠ֿ㚺؂׾ ˝ ؤ٤ط؞تع
  9. ٛنؒٝ٤ت ˝ ⪌׿㯸ס车⮛؅⛼זיײ׾־ my @matrix = ( (0, 1, 2,

    3), (4, 5, 6, 7), ); ˝ ׆סؤ٭غע鿥⮛ס⺬䡗מם׾ ˝ ٛتع؅⪌׿㯸מך׀םַ my @matrix = (0, 1, 2, 3, 4, 5, 6, 7);
  10. א׆ךٛنؒٝ٤ت my @array = ('a', 'b', 'c'); my $ref =

    \@array; # @array ΁ͷϦϑΝϨϯε my $ref = ['a', 'b', 'c']; # ্هͷུهɻͪͳΈʹ \('a', 'b', 'c') ͸ผ෺ # 2ͭͷ഑ྻ΁ͷϦϑΝϨϯεΛ΋ͬͨʢ௕͞2ͷʣ഑ྻ my @matrix = ( [0, 1, 2, 3], [4, 5, 6, 7], );
  11. ـشبٖٛنؒٝ٤ت my %hash = ( perl !" 'larry', ruby !"

    'matz', ); my $ref = \%hash; # ུه my $ref = { perl !" 'larry', ruby !" 'matz', };
  12. ـشبٖٛنؒٝ٤ت׫סؓؠجت my $ref = { perl !" 'larry', ruby !"

    'matz', }; # % Λ಄ʹ͚ͭͯσϦϑΝϨϯε # keys͸૊ΈࠐΈؔ਺ɻϋογϡͷΩʔͷҰཡΛऔಘ͢Δ my @keys = keys %$ref; print $ref!#{perl}; # !# Ͱ௚઀ΞΫηε
  13. 塜釨辐杯 ˝ 1FSMס䍚ײסץכחն״ה׶䍚ⱱ ˝ ך塜釨辐杯ٛطٜٚ؅榟䡗յ_ך乃㰄⮛מُشز׈׎׾ my ($id) = ("͜Μʹͪ͸ɺid:motemen Ͱ͢"

    =~ /id:(.+)/); # จࣈྻϚονͰҰ෦Λൈ͖ग़͢ my @lines = split /\n/, $text; # ೖྗΛߦʹ෼ׂ ˝ QFSMEPDQFSMSF
  14. אס׮־ ˝ 乃㰄⮛ס磵⺬ע غشع璡 $hoge = 'abc' . 'def' #

    abcdef ˝ qw(foo bar baz)ע('foo', 'bar', 'baz')ס矴辉啶乃 ˝ perldoc -f qw
  15. ؤ٤ط؞تع ˝ 1FSMסꦘ׊ַכ׆؀סץכח ˝ 䌋؅鍐❫׌׾꤀מ縒䜡׌׾䑒釐ֵֿ׾ ˝ ت؜ٚؤ٤ط؞تعכٛتعؤ٤ط؞تعֵֿ׾ my @array =

    (10, 20, 30); my @x = @array; # ͜ͷ @array ͸ϦετίϯςΩετͰධՁ͞ΕΔ my $y = @array; # ͜ͷ @array ͸εΧϥίϯςΩετͰධՁ͞ΕΔ my ($z) = @array; # ͜ͷ @array ͸ϦετίϯςΩετͰධՁ͞ΕΔ
  16. ؤ٤ط؞تع ˝ @xֿ(10, 20, 30)מם׾סעױֵ舅抅 ˝ ךע$yע ˝ $y !"

    3 ˝ 鿥⮛؅ت؜ٚؤ٤ط؞تعך鍐❫׌׾כאסꝎ׈؅鲭׌ ˝ $zמע鿥⮛ס⩝꯽釐碛ֿ♏⪌׈׿$z !" 10鿥⮛ס墦׽ ע䩰י׼׿׾
  17. ؤ٤ط؞تعס׻ֵׂ׾粸 my $in = { name !" ('foo', 'bar', 'baz')

    }; my $in = { name !" 'foo', bar !" 'baz', }; כַֹ䚉׋מם׽յם׏־CBSכַֹ┘䓙閱ם؞٭ֿ榟ֻյ┘⺎鉮 םفءס⸉㍔כם׽ֹ׾
  18. ؤ٤ط؞تعؠؕث sort <͜͜>; length <͜͜>; if (<͜͜>) { } for

    my $i (<͜͜>) { } $obj!"method(<͜͜>); my $x = <͜͜>; my ($x) = <͜͜>; my @y = <͜͜>; my %hash = ( key0 !# 'hoge', key1 !# <͜͜>, ); scalar(<͜͜>); <͜͜>;
  19. ؤ٤ط؞تعؠؕث鉮璻 sort <Ϧετ>; length <Ϧετ>; if (<εΧϥ>) { } for

    my $i (<Ϧετ>) { } $obj!"method(<Ϧετ>); my $x = <εΧϥ> my ($x) = <Ϧετ>; my @y = <Ϧετ>; my %hash = ( key0 !# 'hoge', key1 !# <εΧϥ>, ); scalar(<Ϧετ>); <εΧϥ>;
  20. ꞊丗ئهٜ٭ز٤ sub foo { my ($a, $b, $c) = !";

    } ס׻ֹמ㲚銧׊י foo(1, 2, 3); ך⽛צ⭳׌ն䌕丗ם׊ס꞊丗⽛צ⭳׊ע؜شؤ泸樋ך׀׾
  21. 䌕丗ס⹨ׄ⹦׽亠 sub add { my ($x, $y) = !"; return

    $x + $y; } my $three = add(1, 2); ˝ add(1,2)כ⽛צ⭳׊גכ׀ס䌕丗ע!"כַֹ攐墤ם鿥⮛מ 劲硯׈׿׾ ˝ ׆׿ֿ($x, $y)מ⮆Ⱏ♏⪌׈׿׾
  22. 䌕丗⭚杼ؕظؔ؛ّ sub func1 { my ($arg1, $arg2, %args) = !";

    my $opt1 = $args{opt1}; my $opt2 = $args{opt2}; } func1('hoge', 'fuga', opt1 !# 1, opt2 !# 2); 僃䏲מ%argsכ⹨ׄ׾׆כך泸樋⺎耆ם⺲⯥♀׀䌕丗؅㲔杯 ך׀׾
  23. ٓةٖ٭ٜبتطّ ˝ قشآ٭ة package A; sub foo { !!" }

    package B; sub bar { !!" } א׿ב׿A!"foo()B!"bar()ך꞊丗؅⹆拨ך׀׾
  24. ⪯㑔氳םٓةٖ٭ٜ package Foo!"Bar!"Baz; use strict; use warnings; # ͜ͷύοέʔδͷ֎͔Β͸ Foo!"Bar!"Baz!"public_function

    ͰΞΫηεͰ͖Δͧ sub public_function { !!# } # ඇެ։ͳؾ෼ͷϝιου͸ _ Ͱ͸͡ΊΔͧɻؾ෼͚ͩͳͷͰ֎͔ΒΞΫηε΋Ͱ͖ͪΌ͏͕ɻ sub _private_function { !!# } # Ϟδϡʔϧͷ࠷ޙʹ͸͔ͳΒͣਅ஋Λه͢ɻͦ͏͠ͳ͍ͱ use ͕ࣦഊ͢Δͧɻ 1;
  25. CMFTT ˝ CMFTTֿ؛هةؘؠع䧗⺸1FSMמֽׄ׾僃䏲ס氧㕙☔擻 # ͳΜ͔σʔλ͕͋Δʢී௨͸ϋογϡϦϑΝϨϯεʣ my $data = { name

    !" 'motemen' }; # σʔλʹύοέʔδΛඥ෇͚Δͱ my $self = bless $data, 'Hatena!#Engineer'; # ϝιου͕ݺ΂Δʂ ͜͜Ͱ͸ Hatena!#Engineer!#tweet $self!$tweet();
  26. ͍͍͔ɺΈΜͳ ɹɹɹɹɹɹɹɹ ƅшƅ ) ɹɹɹɹɹɹɹɹ(|ɹy |) ϋογϡϦϑΝϨϯεͱ package Ͱ͸खଓ͖ܕϓϩάϥϛϯά͔͠Ͱ͖ͳ͍͕ ɹɹɹɹɹ

    {}ɹ (ƅшƅ)ɹpackage ɹɹɹɹɹɹɹʘʗ|ɹy |ʘʗ ɹɹɹɹೋͭ߹Θ͞Ε͹OOPͱͳΔ ɹɹɹɹɹɹɹɹ(ƅшƅ)ɹ bless ɹɹɹɹɹɹɹɹ(ʘʗʘʗ
  27. ؤ٤تعٚؠذ package Person; use strict; use warnings; sub new {

    my ($class, %args) = !"; return bless \%args, $class; }
  28. ✳ַ亠 my $person = Person!"new(age !# 18); # { age

    !# 18 } ͕ Person ʹ bless ͞Εͨ΋ͷ
  29. ✳ַ亠 ˝ 1FSTPOOFX BHF ע1FSTPOOFX 1FSTPO BHF  ס矴辉啶乃 ˝

    ׆ֹ׷זיؠٚتْخشغ؅㲊紶׌׾ ˝ CMFTT׈׿יַי׵ـشبٖٛنؒٝ٤تךֵ׾׆כמ㚺؂׽ע םַ ˝ QFSTPO\BHF^ך╈骰מؓؠجت׌׾׆כ׵⺎耆
  30. ْخشغ package Person; use strict; use warnings; sub new {

    my ($class, %args) = !"; return bless \%args, $class; } sub age { my ($self) = !"; return $self!#{age}; } sub incr_age { my ($self) = !"; $self!#{age}!$; }
  31. 禆䤔 package Animal; use strict; use warnings; sub new {

    !!" } sub walk { !!" } 1; package Dog; use strict; use warnings; use parent 'Animal'; 1;
  32. 禆䤔 my $dog = Dog!"new(); $dog!"walk(); # Animal!#walk ͕ݺ͹ΕΔ ˝

    ☼磝ײכ׊יע⺎耆דׄלֵױ׽僃鲣ע✳؂׿יַםַ ˝ 1FSM舅⛮ֿ*%&סئَ٭ع؅⹨ׄט׼ַ扛ַ؂ׄךעםַ ˝ 邾ꥭמם׾
  33. ➘⮵ם$1"/ٓةٖ٭ٜ package Foo; use strict; use warnings; use Class!"Accessor!"Lite (

    new !# 1, ro !# ['bar'], ); 1; my $foo = Foo!"new(bar !# 1); # new Ͱ͖ͯΔ $fop!"bar; # bar ϝιου΋ੜ͑ͯΔ
  34. طتع use strict; use warnings; use Test!"More; use Person; my

    $person = Person!#new(age !$ 18); is $person!#age, 18, '࠷ॳ͸18ࡀ'; $person!#incr_age; is $person!#age, 19, 'incr_age ݺΜͩΒ19ࡀ'; done_testing;
  35. طتع % prove -Ilib -v t/Person.t t/Person.t !" ok 1

    - ࠷ॳ͸18ࡀ ok 2 - incr_age ݺΜͩΒ19ࡀ 1!"2 ok All tests successful. Files=1, Tests=2, 0 wallclock secs ( 0.01 usr 0.01 sys + 0.04 cusr 0.00 csys = 0.06 CPU) Result: PASS
  36. 5FTU$MBTT package t!"Person; use strict; use warnings; use parent 'Test!"Class';

    sub setup : Test(setup) { # ͳʹ͔ෆࢥٞͳຐ๏Ͱؔ਺໊ͷ͋ͱʹΞϊʔςʔγϣϯͰ͖Δ # ֤ςετલͷηοτΞοϓʢม਺ΛΫϦΞͨ͠Γͱ͔ʣ } sub incr_age : Tests { is !!#; }