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

Perlに比べてPHPが不便(主観です) ああ…だから僕は…

uzulla
May 21, 2016

Perlに比べてPHPが不便(主観です) ああ…だから僕は…

LT at PHP Conference Fukuoka 2016

uzulla

May 21, 2016
Tweet

More Decks by uzulla

Other Decks in Programming

Transcript

  1. yay • h#ps:/ /github.com/marcioAlmada/yay • Yay is a high level

    PHP preprocessor • Not ready for real world usage yet !
  2. <?php # ͜ͷʮmacroʯͱ͍͏ॴΛyay͕ࢀর͢Δ macro { say ·string()·message } >> {

    echo ·message; echo PHP_EOL; } # ͜͜·Ͱmacro say "say!"; say "yes!";
  3. <?php macro { unless (···expression) { ···body } } >>

    { if (! (···expression)) { ···body } } $isSuccess = true; unless($isSuccess){ echo('failed'); }
  4. <?php macro { foreach ( T_LNUMBER·S .. T_LNUMBER·E as T_VARIABLE·INDEX

    ) { ···body } } >> { foreach ( range(T_LNUMBER·S , T_LNUMBER·E) as T_VARIABLE·INDEX ) { ···body } } foreach( 1 .. 10 as $_ ){ echo $_; }
  5. foreach( 1 .. 10 as $_ ){ echo $_; }

    // ↓ foreach ( range(1 , 10) as $_ ){ echo $_; }
  6. <?php macro { foreach ( T_LNUMBER·S .. T_LNUMBER·E as T_VARIABLE·INDEX

    ) { ···body } } >> { $__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 $_; }
  7. 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 $_; }
  8. ਖ਼نදݱϚονͱஔ׵ • Ϛον • if( "123" =~ /2/ ){ }

    • ஔ׵ • $abc =~ s/a/A/; • ݸਓతʹ͸݁ߏ޷͖ • ʮPerlه߸ଟਿʯ ʮperlopͰάάΕʯ
  9. <?php macro { T_VARIABLE·A =~ /T_STRING·B/ } >> { preg_match(

    '/'. ··stringify(T_STRING·B) .'/u', T_VARIABLE·A ) } if($abc =~ /a/){ echo 1; }
  10. if($abc =~ /a/){ echo 1; } // ↓ if(preg_match( '/'.

    'a' .'/u' ,$abc )){ echo 1; } # օ͞Μ͕͍͍͍ͨ͜ͱ͸ղΔ
  11. <?php macro { T_VARIABLE·A =~ s/T_STRING·B/T_STRING·C/ } >> { 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;
  12. <?php macro { if( T_VARIABLE·A./T_STRING·B/ ){ ···body } } >>

    { if( preg_match( '/'. ··stringify(T_STRING·B) .'/u' , T_VARIABLE·A ) ) { ···body } } $abc = 'abc'; if($abc./a/){ echo 1; }
  13. <?php macro { ... } >> { throw new \Exception

    ('Unimplemented'); } if(1){ ... }
  14. <?php macro { { ···body } } >> { (

    function( $arg ) { extract( $arg ); ···body } ) ( get_defined_vars() ) ; } $abc = 'abc'; { $abc = 'def'; echo $abc, PHP_EOL; } echo $abc, PHP_EOL;
  15. { $abc = 'def'; } // ↓ ( function( $arg·cfSNIPda

    ) { extract( $arg·cfSNIPda ); $abc = 'def'; } ) ( get_defined_vars() ) ;
  16. <?php macro { use ·ns()·type ; } >> { 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;
  17. <?php macro { use strict; } >> { set_error_handler( function($errno,

    $errstr, $errfile, $errline){ throw new \Error($errstr); } ); } macro { use warnings; } >> { error_reporting(-1); } use strict; use warnings; echo $a;
  18. use strict; use warnings; // ↓ <?php set_error_handler( function( $errno·cfSNIPda,

    $errstr·cfSNIPda, $errfile·cfSNIPda, $errline·cfSNIPda ){ throw new \Error($errstr·cfSNIPda); } ); error_reporting(-1);
  19. <?php macro { (···condition) -> { ···body } } >>

    { function ( ···condition ) { ···body } } $a = ($v)->{ echo $v; }; $a('abc');
  20. $a = ($v)->{ echo $v; }; // ↓ $a =

    function ( $v ) { echo $v; };
  21. Defined-Or, //= ԋࢉࢠ • $a //= 'default'; • ʢ//͸??ͱಉ͡ʣ •

    ຊؾͰཉ͍͠ • 7.xͰདྷΔʢ͚ͲઌऔΓͱ͍͏͜ͱͰʣ => https://wiki.php.net/rfc/ null_coalesce_equal_operator
  22. <?php macro { T_VARIABLE·A ??= } >> { T_VARIABLE·A =

    T_VARIABLE·A ?? } $a ??= 123; echo $a;
  23. <?php macro { split } >> { mb_split } macro

    { join } >> { implode } print_r( split(",", "a,b,c") ); print_r( join(",", ["a","b","c"]) ); # PHP7Ͱsplit()͕ͳ͘ͳͬͯ΋ # mb_split͸ͷ͍ͬͯ͜ΔΜͩΑͳ͋…