Perlに比べてPHPが不便(主観です) ああ…だから僕は…
by
uzulla
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
PerlʹൺͯPHP͕ෆศ ʢओ؍Ͱ͢ʣ ͋͋…͔ͩΒ… • date: 20160521 • at: PHPΧϯϑΝϨϯεԬ 2016 • speaker: uzulla
Slide 2
Slide 2 text
ࣗݾհ • ౦ͷํͷ͏ͣΒ(uzulla)Ͱ͢ • ମܕతʹʮLʢΤϧʣʯͳํ Ͱ͢ • ʢԬͷudzura͞Μମܕ ʮRʢϨΪϡϥʔʣʯͰ͢ Ͷʣ • Perlք۾ͰPHPͷਓͱ͍Θ Ε·͕͢ɺPHPք۾ͰPerl ͷਓͱ͍ΘΕ͍ͯΔؾ͕͠· ͢
Slide 3
Slide 3 text
Perlʹൺͯ PHP͕ෆศ ʢओ؍Ͱ͢ʣ
Slide 4
Slide 4 text
ʮPerlͷΧοίΠΠϠπ ʢओ؍ʣ ΛPHPͰ͔͍͍ͭͨ ͧʂʂʯ
Slide 5
Slide 5 text
͋͋…͔ͩΒ…
Slide 6
Slide 6 text
ΠΤʔΠʂ
Slide 7
Slide 7 text
yay • h#ps:/ /github.com/marcioAlmada/yay • Yay is a high level PHP preprocessor • Not ready for real world usage yet !
Slide 8
Slide 8 text
ϓϦϓϩηοαͱʁ • Cͱ͔Ͱ͓ೃછΈ • ίϯύΠϧ࣌ʹίʔυΛಈతʹม • ఆɺϚΫϩஔ • yayPHPίʔυΛม͠ɺPHPίʔυͱͯ͠ग़ྗ ͢ΔτϥϯεύΠϥΈ͍ͨͳͷ • ࠓճPHPͷߏจΛվ͢Δͷʹ͔͍ͭ·͢
Slide 9
Slide 9 text
ͱΓ͋͑ͣࢼ͢πʔϧ • github.com/uzulla/yay-tester.php • evalͯ͠ΔͷͰةݥͰ͢!खݩͰ͔͍ͭ·͠ΐ͏ • ʮ҆શͳϨϯαόͳΒ͏͔͍͍ͯ͝͠…ʁʯ ʢԥΒΕͦ͏ͳͷͰࣗॏ͠·ͨ͠ʣ • PHP>=7 ʢevalΛtryʙcatch (\Throwable $e) {}Ͱ ଈࢮճආʣ
Slide 10
Slide 10 text
say • say "hogehoge"; • sayechoΑΓҰจࣈগͳ͍ʂ • վߦ͕͘ͷ͕࠷ߴʹศརʂ • ʮPerl6͡ΌΜʯ ʮp5Ͱuse feature 'say'͢Ε…ʯ
Slide 11
Slide 11 text
> { echo ·message; echo PHP_EOL; } # ͜͜·Ͱmacro say "say!"; say "yes!";
Slide 12
Slide 12 text
say "say!"; say "yes!"; // ↓ echo "say!"; echo PHP_EOL; echo "yes!"; echo PHP_EOL;
Slide 13
Slide 13 text
say! yes!
Slide 14
Slide 14 text
unless • unless($flag){} • if(!$flag){}ͱಉ͡ • ʮ!Λॻ͚͍͍͡Όͳ͍ʯ ʮif( ! isKakin())Έ͍ͨͳͷࢁଓ͘ͱͭΒ͍…ʯ ʮisHikakin()Λͭ͘Ε͑͑Μʯ ʮͳΔ΄Ͳʁʯ
Slide 15
Slide 15 text
> { if (! (···expression)) { ···body } } $isSuccess = true; unless($isSuccess){ echo('failed'); }
Slide 16
Slide 16 text
$isSuccess = true; unless($isSuccess){ echo('failed'); } // ↓ $isSuccess = false; if (! ($isSuccess)) { echo('failed'); }
Slide 17
Slide 17 text
failed
Slide 18
Slide 18 text
range ԋࢉࢠ • for(1 .. 10){ print $_; } • ਖ਼͔ͳΓཉ͍͠
Slide 19
Slide 19 text
> { foreach ( range(T_LNUMBER·S , T_LNUMBER·E) as T_VARIABLE·INDEX ) { ···body } } foreach( 1 .. 10 as $_ ){ echo $_; }
Slide 20
Slide 20 text
foreach( 1 .. 10 as $_ ){ echo $_; } // ↓ foreach ( range(1 , 10) as $_ ){ echo $_; }
Slide 21
Slide 21 text
12345678910
Slide 22
Slide 22 text
range ԋࢉࢠ ͦͷ̎ • ʮrange()Ͱforeachͱ͔μϝͰ͠ΐʯ ʮ͍ʯ • ʢੲͷPerlPHPಉ༷ʹڊେྻ͕Ͱ͖͍ͯͨ ʢΒ͍͠ʣʣ • ʢࠓճ࣮͠ͳ͍͚Ͳ('a' .. 'z')ͳͲ
Slide 23
Slide 23 text
> { $__itelatorGenerator = itelatorGenerator(T_LNUMBER·S, T_LNUMBER·E); foreach ( $__itelatorGenerator as T_VARIABLE·INDEX ) { ···body } } function itelatorGenerator($init, $fin){ $num = $init; while(1){ if( $fin < $num ) { return $num++; } yield $num++; } } foreach( 1 .. 10 as $_ ){ echo $_; }
Slide 24
Slide 24 text
function itelatorGenerator($init, $fin){ $num = $init; while(1){ if( $fin < $num ) { return $num++; } yield $num++; } } // ͜͜·Ͱڞ௨ foreach( 1 .. 10 as $_ ){ echo $_; } // ↓ $__itelatorGenerator·cfcd208495d565ef66e7dff9f98764da = itelatorGenerator(1, 10); foreach ( $__itelatorGenerator·cfcd208495d565ef66e7dff9f98764da as $_ ) { echo $_; }
Slide 25
Slide 25 text
12345678910
Slide 26
Slide 26 text
ਖ਼نදݱϚονͱஔ • Ϛον • if( "123" =~ /2/ ){ } • ஔ • $abc =~ s/a/A/; • ݸਓతʹ݁ߏ͖ • ʮPerlه߸ଟਿʯ ʮperlopͰάάΕʯ
Slide 27
Slide 27 text
> { preg_match( '/'. ··stringify(T_STRING·B) .'/u', T_VARIABLE·A ) } if($abc =~ /a/){ echo 1; }
Slide 28
Slide 28 text
if($abc =~ /a/){ echo 1; } // ↓ if(preg_match( '/'. 'a' .'/u' ,$abc )){ echo 1; } # օ͞Μ͕͍͍͍ͨ͜ͱղΔ
Slide 29
Slide 29 text
> { T_VARIABLE·A = preg_replace( '/'. ··stringify(T_STRING·B) .'/u', ··stringify(T_STRING·C) , T_VARIABLE·A ) } $abc = 'abc'; $abc =~ s/a/v/; echo $abc;
Slide 30
Slide 30 text
$abc =~ s/a/v/; // ↓ $abc = preg_replace( '/' . 'a' . '/u', 'v' , $abc );
Slide 31
Slide 31 text
༨ஊ:JS෩ਖ਼نදݱϚον • if($abc./a/){ ... } • ʮOOPͬΆ͍ʂʯ ʮݩʑΦϒδΣΫτࢦݴޠ͔ͩͧͬ͠Γ͠Ζʯ • ʮPHPͳΒ −>Ͱʁʯ ʮͤͳʯ
Slide 32
Slide 32 text
> { if( preg_match( '/'. ··stringify(T_STRING·B) .'/u' , T_VARIABLE·A ) ) { ···body } } $abc = 'abc'; if($abc./a/){ echo 1; }
Slide 33
Slide 33 text
if($abc./a/){ echo 1; } // ↓ if( preg_match( '/'. 'a' .'/u' , $abc ) ){ echo 1; }
Slide 34
Slide 34 text
yadayada • ... • ʮޙͰ࣮͢Δ͔Β...ʯͱ͍͏࣌ͷʮ...ʯ • ౸ୡ͢Δͱɺྫ֎্͕͕Δ • //TODOΑΓΘ͔Γ͍͢…͔…͠Εͳ͍
Slide 35
Slide 35 text
> { throw new \Exception ('Unimplemented'); } if(1){ ... }
Slide 36
Slide 36 text
if(1){ ... } // ↓ if(1){ throw new \Exception ('Unimplemented'); }
Slide 37
Slide 37 text
block scope • {ʙ} • ϒϩοΫͷதͰείʔϓ͕ग़དྷΔ • ͋ΔҙຯҰ൪ϚΫϩͬΆ͍ • ΊͬͪΌ΄͍͠ʂʂʂ
Slide 38
Slide 38 text
> { ( function( $arg ) { extract( $arg ); ···body } ) ( get_defined_vars() ) ; } $abc = 'abc'; { $abc = 'def'; echo $abc, PHP_EOL; } echo $abc, PHP_EOL;
Slide 39
Slide 39 text
{ $abc = 'def'; } // ↓ ( function( $arg·cfSNIPda ) { extract( $arg·cfSNIPda ); $abc = 'def'; } ) ( get_defined_vars() ) ;
Slide 40
Slide 40 text
def abc
Slide 41
Slide 41 text
use • ʮPHPͷuseͬͯͳΜͳͷɺಡΈࠐΜͰ͘Εͳ͍ͷ Α͘Θ͔Βͳ͍Μ͚ͩͲ…ʯʢ༑ਓͷJVMܥϓϩ άϥϚஊʣ ʮͨɺ͔ͨ͠ʹ…ʯ • useͰຊʹrequire_once͢Εղܾ
Slide 42
Slide 42 text
> { use ·type ; __require( ··stringify(·type) ) ; } function __require($class){ $array = explode('\\', ltrim($class, '\\')); $path = implode('/', $array); $filename = $path.'.php'; require_once($filename); } use \This\Is\Test;
Slide 43
Slide 43 text
use \This\Is\Test; // ↓ use \This\Is\Test ; __require( '\This\Is\Test' ) ;
Slide 44
Slide 44 text
use strict; use warnings; • ͍ܰΤϥʔͰऴྃ͢Δ • ͍͋͠ॴͰܯࠂ͢Δ • ʢຊ͏ͪΐͬͱҙຯ͕͋Δʣ • ʢηοτ͍ͯ͠ΔΤϥʔϋϯυϥ࡞๏ݸਓతझ ຯͰ͢ʣ
Slide 45
Slide 45 text
> { set_error_handler( function($errno, $errstr, $errfile, $errline){ throw new \Error($errstr); } ); } macro { use warnings; } >> { error_reporting(-1); } use strict; use warnings; echo $a;
Slide 46
Slide 46 text
use strict; use warnings; // ↓
Slide 47
Slide 47 text
·ͱΊ • yayͬͯ݁ߏศརͰʂʢʁʣ • ʮҰਓϓϩδΣΫτझຯͳΒͳʂʯ • ஔͱ͔Ͱͳ͍ͷͰɺ ʢͪΌΜͱॻ͚ʣ͔ͳΓյΕͮΒ͍ • ग़ྗ͞ΕΔͷɺຊͷPHPͳͷͰ҆৺ʢʁʣ • ୯७ͳϧʔλʔఆٛͷDSLͳΒͭ͘Ε·ͨ͠
Slide 48
Slide 48 text
ຊʹΔਓͷίϝϯτ • yayɺυΩϡϝϯτ͕ςετίʔυ͔͠ͳ͍ͷͰௐ Δͷ͕େม • τʔΫϯछͷT_VARIABLE github/nikic/PHP-Parser • ύʔαʔͷ·ls github/marcioAlmada/yay/src/ parsers.php • ʮͱʹ͔࣮͘ͱςετίʔυΛݟΔΜͩʯ
Slide 49
Slide 49 text
• ·ͱ.ͱɾΛؒҧ͑ͯେม • middle dot • ʮ·(00B7)ͬͯɺීஈͲ͏ͬͯೖྗ͢ΔΜ…ʯ • ʮதࠇΛ·(00B7)ʹม͢Δpreprocessor͕ඞཁͰ ʯ
Slide 50
Slide 50 text
__END__
Slide 51
Slide 51 text
__halt_compiler();
Slide 52
Slide 52 text
ʮyapc8oji Ͱάάͬͯ͘ΕཔΉʯ τʔΫԠืͯ͘͠Εʂʂ̍
Slide 53
Slide 53 text
͓·͚ɿshort closure • ( )->{ }
Slide 54
Slide 54 text
{ ···body } } >> { function ( ···condition ) { ···body } } $a = ($v)->{ echo $v; }; $a('abc');
Slide 55
Slide 55 text
$a = ($v)->{ echo $v; }; // ↓ $a = function ( $v ) { echo $v; };
Slide 56
Slide 56 text
Defined-Or, //= ԋࢉࢠ • $a //= 'default'; • ʢ//??ͱಉ͡ʣ • ຊؾͰཉ͍͠ • 7.xͰདྷΔʢ͚ͲઌऔΓͱ͍͏͜ͱͰʣ => https://wiki.php.net/rfc/ null_coalesce_equal_operator
Slide 57
Slide 57 text
> { T_VARIABLE·A = T_VARIABLE·A ?? } $a ??= 123; echo $a;
Slide 58
Slide 58 text
$a ??= 123; echo $a; // ↓ $a = $a ?? 123; echo $a;
Slide 59
Slide 59 text
splitͱjoin • ʮͲ͏ͯ͠Explode/Implode͕׳Εͳ͍ʯ ʮ׳ΕΖʯ ʮͳͥจࣈΛ૿͢ํʹߦͬͨͷ͔ʯ ʮఘΊΖʯ • PHP7Ͱ͍ͭʹফ໓͠·ͨ͠Ͷ
Slide 60
Slide 60 text
> { mb_split } macro { join } >> { implode } print_r( split(",", "a,b,c") ); print_r( join(",", ["a","b","c"]) ); # PHP7Ͱsplit()͕ͳ͘ͳͬͯ # mb_splitͷ͍ͬͯ͜ΔΜͩΑͳ͋…
Slide 61
Slide 61 text
split(",", "a,b,c"); join(",", ["a","b","c"]); // ↓ mb_split(",", "a,b,c"); implode(",", ["a","b","c"]);