Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Development and practical use of CLI in perl 6

545 views

Published on

YAPC::Tokyo 2019 トーク資料
Slide of my talk about development of command line interface in perl 6.
https://yapcjapan.org/2019tokyo/talks.html#/detail/12

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Development and practical use of CLI in perl 6

  1. 1. Perl 6 by risou at YAPC::Tokyo 2019
  2. 2. ▸ risou ▸ ID "risouf" ▸ ▸ Perl 6 😎 ▸ Perl Ruby Go
  3. 3. ▸ ▸ Perl 6 CLI ▸ Perl 6 ▸ URI ▸ ▸ Perl 6 ▸ Web
  4. 4. Perl 6 ▸ Linux ▸ ▸ 2018 10 OK $ wget https://rakudo.perl6.org/downloads/star/rakudo-star-2018.10.tar.gz $ tar xfz rakudo-star-2018.10.tar.gz $ cd rakudo-star-2018.10 $ perl Configure.pl --gen-moar --make-install --prefix ~/rakudo
  5. 5. Perl 6 ▸ MacOS ▸ brew install rakudo-star ▸ homebrew Linux ▸ Windows ▸ msi 2018 10 ▸ https://rakudo.perl6.org/downloads/star/rakudo-star-2018.10-x86_64%20(JIT).msi
  6. 6. Perl 6 ▸ Docker ▸ ▸ rakudobrew ▸ https://github.com/tadzik/rakudobrew ▸ p6env ▸ https://github.com/skaji/p6env $ docker pull rakudo-star $ docker run -it rakudo-star
  7. 7. Command Line Interface
  8. 8. Perl 6 MAIN
  9. 9. MAIN ▸ #!/usr/bin/env perl6 sub MAIN() { say "sub MAIN called."; }
  10. 10. MAIN ▸ ▸ #!/usr/bin/env perl6 say "Hello Perl World!";
  11. 11. MAIN ▸ …… #!/usr/bin/env perl6 say "Hello Perl World! (before MAIN sub)"; sub MAIN() { say "Hello Perl 6 World!"; } say "Hello Perl World! (after MAIN sub)";
  12. 12. MAIN ▸ ▸ 
 MAIN $ perl6 hello_world.p6 Hello Perl World! (before MAIN sub) Hello Perl World! (after MAIN sub) Hello Perl 6 World!
  13. 13. MAIN ▸ MAIN ▸ ▸ Usage
  14. 14. MAIN ▸ ▸ Usage #!/usr/bin/env perl6 sub MAIN($param) { say "Hello $param World!"; } $ perl6 hello_param_world.p6 Usage: hello_param_world.p6 <param>
  15. 15. MAIN ▸ ▸ Usage #!/usr/bin/env perl6 sub MAIN(Int $num) { say "num: $num"; } $ perl6 print_num.p6 string Usage: print_num.p6 <num>
  16. 16. MAIN ▸ multi sub #!/usr/bin/env perl6 multi sub MAIN(Int $num) { say "num: $num"; } multi sub MAIN(Str $str, Int $num) { say "str: $str"; say "num: $num"; }
  17. 17. ▸ MAIN @*ARGS #!/usr/bin/env perl6 sub MAIN(Str $str, Int $num) { say "str: $str"; say "num: $num"; } say @*ARGS.elems; say @*ARGS.perl; $ perl6 yapc.p6 YAPC 2019 2 ["YAPC", "2019"] str: YAPC num: 2019
  18. 18. Usage...? Usage
  19. 19. Usage...? Perl 6 USAGE
  20. 20. USAGE ▸ #!/usr/bin/env perl6 sub MAIN(Str $str, Int $num) { say "str: $str"; say "num: $num"; } sub USAGE() { say "sub USAGE called."; } $ perl6 usage.p6 sub USAGE called.
  21. 21. USAGE ▸ ▸ ▸ :to #!/usr/bin/env perl6 sub MAIN(Str $str, Int $num) { say "str: $str"; say "num: $num"; } sub USAGE() { print q:to/END/; Usage: heredocs.p6 <str> <num> please set "str" first. END }
  22. 22. USAGE ▸ Usage ▸ $*USAGE #!/usr/bin/env perl6 sub MAIN(Str $str, Int $num) { say "str: $str"; say "num: $num"; } sub USAGE() { say $*USAGE.perl; } $ perl6 var_usage.p6 "Usage:n var_usage.p6 <str> <num> "
  23. 23. USAGE ▸ Usage ▸ #!/usr/bin/env perl6 #| only 1 parameter multi sub MAIN(Int $num) { say "num: $num"; } #| need 2 parameters, first parameter's type is Str, second is Int multi sub MAIN(Str $str, Int $num) { say "str: $str"; say "num: $num"; }
  24. 24. USAGE ▸ Usage ▸ MAIN Usage $ perl6 declarator.p6 Usage: declarator.p6 <num> -- only 1 parameter declarator.p6 <str> <num> -- need 2 parameters, first parameter's type is Str, second is Int
  25. 25. ▸ #| ▸ #| ▸ #= ▸ Perl 6 pod #| only 1 parameter multi sub MAIN(Int $num) { say "num: $num"; }
  26. 26. ▸ CLI 
 ▸ ……
  27. 27. Perl 6 
 Perl
  28. 28. ▸ #!/usr/bin/env perl6 my $fh = open "README.md"; my $contents = $fh.slurp; $fh.close; #!/usr/bin/env perl6 my $fh = open "output.txt", :w; $fh.print: "Hello World!"; $fh.close;
  29. 29. ▸ slurp ▸ ▸ ▸ my $contents = $fh.slurp; my $contents = slurp "README.md";
  30. 30. ▸ ▸ $fh.slurp IO::Handle ▸ slurp "filename" IO role ▸ open/close
  31. 31. ▸ slurp ▸ ▸ spurt ▸ slurp open/close 

  32. 32. ▸ ▸ IO lines for "README.md".IO.lines -> $line { say $line; } my $fh = open "README.md"; for $fh.lines -> $line { say $line; } $fh.close;
  33. 33. ▸ "README.md".IO ▸ .IO 
 IO::Path ▸ IO::Path.new("README.md") #!/usr/bin/env perl6 my $fh = open "README.md"; say $fh.perl; # IO::Handle say $fh.IO.perl; # IO::Path say "README.md".perl; # Str say "README.md".IO.perl; # IO::Path
  34. 34. IO::Path ▸ path #!/usr/bin/env perl6 my $readme = "README.md".IO; say $readme.perl; say $readme.CWD; # get absolute path say $readme.path; # get relative path say $readme.basename; # get file name say $readme.extension; # get extension say $readme.dirname; # get directory by relative path say $readme.e; # exist? say $readme.d; # directory? say $readme.f; # file? say $readme.s; # size
  35. 35. ▸ zef ▸ etc... $ zef search Template ===> Found 78 results ... $ zef install Template6 $ zef uninstall Template6
  36. 36. ▸ zef search …… ▸ Perl 6 Modules ▸ https://modules.perl6.org/
  37. 37. ▸ use #!/usr/bin/env perl6 use Template6; my $t6 = Template6.new;
  38. 38. ▸ %*ENV #!/usr/bin/env perl6 for %*ENV.keys -> $key { say "$key: " ~ %*ENV{$key}; }
  39. 39. ▸ …… ▸ mi6 ▸ ▸ YAPC $ zef install App::Mi6
  40. 40. Perl 6 ▸ Perl 6 ▸ https://perl6intro.com/ja/ ▸ ▸ https://perl6intro.com ▸ Perl 6 Documentation ▸ https://docs.perl6.org/ ▸
  41. 41. Perl 6 CLI ▸ Docker ▸ MAIN USAGE ▸ I/O 
 Perl 6 ▸ 

  42. 42. Perl 6 ▸ …… ▸ Perl 6 ▸ 
 Perl 6 
 

  43. 43. Perl 6 ▸ ▸ "50ms or die." ……
  44. 44. .perl ▸ Mu ▸ ▸ Data::Dumper ▸ Mu ▸ Perl 6 ▸ Java Object
  45. 45. Web ▸ …… ▸ YAPC 
 "p6-Crust"

×