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

Perl 5 OOP機構30年史 - Perl 5's OOP Mechanism over ...

moznion
September 18, 2024

Perl 5 OOP機構30年史 - Perl 5's OOP Mechanism over the past 30 years

YAPC::Hakodate 2024 非公式予習会の資料です。
https://smartbank.connpass.com/event/328545/

moznion

September 18, 2024
Tweet

More Decks by moznion

Other Decks in Programming

Transcript

  1.  CMFTT package User; use strict; use warnings; use utf8;

    sub new { my ( $ class, $args) = @_ ; return bless { name => $args->{name}, email => $args->{email}, }, $class; } sub get_name { my ( $ self) = @_ ; return $self->{name}; } 1;  ಋೖ 1FSM   QBDLBHF͕Ϋϥεతʹ;Δ·͍ɺ CMFTTͰσʔλ SFGFSFODF Λ QBDLBHFʹඥ෇͚  NFUIPE΋ఆٛͰ͖Δ  ϓϦϛςΟϰͳ001
  2.  CMFTT package User; use strict; use warnings; use utf8;

    sub new { my ( $ class, $args) = @_ ; return bless { name => $args->{name}, email => $args->{email}, }, $class; } sub get_name { my ( $ self) = @_ ; return $self->{name}; } 1;  ಋೖ 1FSM   QBDLBHF͕Ϋϥεతʹ;Δ·͍ɺ CMFTTͰσʔλ SFGFSFODF Λ QBDLBHFʹඥ෇͚  NFUIPE΋ఆٛͰ͖Δ  ϓϦϛςΟϰͳ001
  3.  DMBTT use strict; use warnings; use feature 'class'; no

    warnings qw/exper im ental::class/; class User { field $name :param; field $email :param; method get_name { return $name; } }  ಋೖ 1FSM   ͍ΘΏΔී௨ͬΆ͍DMBTTߏจ  fi FME΍NFUIPE΋αϙʔτ  ͝ཡͷͱ͓ΓFYQFSJNFOUBM
  4. 

  5. ৭ʑ $MBTT4USVDU $MBTT"DDFTTPS $MBTT"DDFTTPS'BTU $MBTT"DDFTTPS'BTUFS $MBTT"DDFTTPS-JUF $MBTT$MBTT $MBTT$POUSBDU $MBTT4JNQMF $MBTT&BTZ

    $MBTT5JOZ $MBTT4UE $MBTT(FOFSBUF $MBTT.FUIPE.BLFS 3VCZJTI$MBTT .PPTF .PVTF .PP .P . .PPT .PVTTF .PN %JPT .V 4QJ ff Z 3PMF5JOZ .PKP#BTF .01 $MBTT.01 $PS 1FSM 3BLV  BOEFUD 
  6. $MBTT4USVDU   ಋೖ 1FSM   QBDLBHFʹରͯ͠NFNCFSΛඥ෇͚Δ ه๏Λαϙʔτ 

    ΞΫηα΋ࣗಈͰੜ͑ͯ͘Δ  ίΞϞδϡʔϧ package User; use Class::Struct; struct(User => { name => '$', email => '$', }); # ͜Μͳײ͡Ͱ࢖͑Δ my $user = User->ne w ( name => 'john', email => '[email protected]', ); say $user->name; # john $user->name('doe'); say $user->name; # doe
  7. $MBTT"DDFTTPS   ϦϦʔε  QBDLBHFʹରͯ͠ΞΫηαΛ ಈతʹੜ੒͢Δ  ͞·͟·ͳߴ଎ɾܰྔͳ೿ੜ͕͋Δ 

    $MBTT"DDFTTPS'BTU  $MBTT"DDFTTPS'BTUFS  $MBTT"DDFTTPS-JUF package User; use base q w (Class::Accessor); User->follo w_ best_practice; User->mk_accessors(q w (name email)); # ͜͏͍͏;͏ʹ࢖͑Δ my $user = User->ne w ({ name => 'john', email => '[email protected]', }); say $user->{name}; # john $user->set('name', 'doe'); # getterΛհͯ͠ΞΫηε΋Ͱ͖Δ say $user->get('name'); # doe
  8. .PPTF   ϦϦʔε   ຊ֨తͳΦϒδΣΫτγεςϜ͕஀ੜ  3BLV ౰࣌1FSM

    ͷΞΠσΞΛഈआ  .01ίϯηϓτ package User; use Moose; has 'name' => (is => 'rw', isa => 'Str'); has 'email' => (is => 'rw', isa => 'Str'); sub greet { my $self = shift; return "hello, ". $ self->name; } # ͜͏͍͏;͏ʹ࢖͑Δ my $user = User->ne w ({ name => 'john', email => '[email protected]', }); say $user->name; # john $user->name('doe'); say $user->name; # doe say $user->greet; # hello, doe
  9. .01 .FUB0CKFDU1SPUPDPM   1FSMͱ1FSMͷ001ίϯηϓτ  Ϋϥε͸ΦϒδΣΫτ  ΫϥεʹରԠ͢ΔϝλΦϒδΣΫτͰ͋ΔϝλΫϥε͕ଘࡏ͢Δ 

    Ϋϥεͷৼ෣͍Λఆٛ͢Δ  ϝλΫϥεΛ௨͢͜ͱͰϝιου΍ଐੑͷ௥Ճ࡟আΛಈతʹߦͳ͑Δ  ଐੑɺϝιουɺϩʔϧ NJYJO ɺϝιουϞσΟϑΝΠΞͳͲۙ୅తͳ 001ػߏΛ۩උ  .PPTF଒͸͜ͷ.01Λ࣮૷͍ͯ͠Δ
  10. .PPTF଒   .PPTFΦϦδφϧɻॏ͍ɾ஗͍ͳͲͱݴΘΕ͍ͯͨ ࠓ͸஌Βͳ͍   .PVTF.PPTFޓ׵ɻ94Ͱॻ͔Ε͍ͯΔͨΊͱͯ΋଎͍ɻ  .PP.PPTFޓ׵

    αϒηοτ ɻܰྔͳ11࣮૷ɻΑ͘࢖ΘΕ͕ͪɻ  .P.PPͷߋʹϛχϚϧͳόʔδϣϯ  .ۃΊ͍ͯܰɻͳͥͳΒԿ΋࣮૷͞Ε͍ͯͳ͍ͨΊʜʜ  δϣʔΫϞδϡʔϧ
  11. .PPTF଒   .PPTFΦϦδφϧɻॏ͍ɾ஗͍ͳͲͱݴΘΕ͍ͯͨ ࠓ͸஌Βͳ͍   .PVTF.PPTFޓ׵ɻ94Ͱॻ͔Ε͍ͯΔͨΊͱͯ΋଎͍ɻ  .PP.PPTFޓ׵

    αϒηοτ ɻܰྔɻΑ͘࢖ΘΕ͕ͪɻ  .P.PPͷߋʹϛχϚϧͳόʔδϣϯ  .ۃΊ͍ͯܰɻͳͥͳΒԿ΋࣮૷͞Ε͍ͯͳ͍ͨΊʜʜ  δϣʔΫϞδϡʔϧ
  12. ༨ஊ   .PKPMJDJPVT͸.01ϕʔεͷ001Λࣗલ࣮૷͍ͯ͠Δ .PKP#BTF   ྲྀੴͷ43*ޚେͰ͋Δ  ࠓ

    ೥Ҏ߱ Ͱ͸$MBTT"DDFTTPS΋.PPTFϥΠΫͳߏจΛఏڙ͍ͯ͠Δ ஌Βͳ͔ͬͨ
  13. DMBTTΛೖΕΑ͏Ωϟϯϖʔϯ   ͝ཡͷΑ͏ʹ 1FSMͷ001͸ϢχʔΫ  ৽نϢʔβʔʹͱͬͯ௚ײతͰͳ͍৔߹͕͋Δ  ݱ୅తͳ001ػߏͷχʔζ͸े෼ཧղͰ͖Δ 

    .01΋͋Δ͠$PS΋͋Δ͠3BLVͰͷ࣮੷΋͋Δ  ͱ͍͏ͱ͜Ζ͔Β1FSMʹDMBTTߏจ͕࣮૷͞ΕΔӡͼʹ  ೥ʹ଴๬ͷϦϦʔε
  14. 1FSM001ʹର͢Δॴײ   CMFTTΛ࢖ͬͨͬͯྑ͍  $MBTT"DDFTTPS Λ࢖ͬͨͬͯྑ͍  .PVTF΍.PPΛ࢖ͬͨͬͯྑ͍ 

    DMBTTΛ࢖ͬͨͬͯྑ͍  ͕ɺݸਓతʹ͸FYQFSJNFOUBM͕֎Ε͔ͯΒɺͦͯ͠3PMF͕࣮૷͞Ε͔ͯΒ ͱ͍͏;͏ʹࢥ͍ͬͯΔ  ͦΕ͕ຬ଍͞ΕͨΒDMBTTߏจʹ౷Ұ͍ͨ͠ؾ͕͢Δ  5.5085%* ΍Γํ͸͍͘ΒͰ΋͋Δ
  15. ·ͱΊ   1FSM001ͷมભ  CMFTT  $MBTT"DDFTTPS଒  .PPTF଒

     ϙετϞμϯ଒  DMBTTߏจ  DMBTTߏจ͕೥ʹಋೖ͞Εͨ  ࠓޙʹظ଴  1FSMʹ΋৽͍͠ػೳ͸ͪΌΜͱೖ͍ͬͯΔͧʂʂʂ
  16. 2