Learning From Ruby (Yapc Asia)

3,090 views

Published on

Introducing several of my Perl5 hacks of bring some Ruby features into Perl5.

Published in: Technology, Education
1 Comment
5 Likes
Statistics
Notes
No Downloads
Views
Total views
3,090
On SlideShare
0
From Embeds
0
Number of Embeds
17
Actions
Shares
0
Downloads
56
Comments
1
Likes
5
Embeds 0
No embeds

No notes for slide
  • Before we learn what we’re learning, let’s learn it first.
  • If you google “Perl 6 Logo”, you get this picture.
    Ruby is so awesome that people once think that we don’t need Perl 6 anymore.
  • I want to start by telling you my learnings from the language aspect
  • by Simon Cozens.
  • After seeing rubyisms, I asked my self a question:
  • Can Perl programs have the same look and feel of Ruby programs?
  • There are several good parts of Ruby that I like...
  • The self keyword – It doesn’t always mean “the current object” like in java.
  • Those baz in there refers to different baz method.
  • The ‘yield’ there means to invoke call the given block as a method.
  • One good use: Markaby
  • Another one Hot cocoa.
  • Ruby primitive data types are all Objects
  • It requires the statements to be written nicely, but it’s nice not having to write semicolons sometimes.
  • Now you can define a class with keyword “class”
  • and define instance methods with keyword “def”
  • plus function prototype
  • See also that module
  • Export “Array” method as the constructor of Rubyish::Array.
  • boolean keywords returns the singleton object of their classes
  • Ruby hash has a lovely way to lazily initialize its content.
  • We have Memoize instead of being Lazy. But it doesn’t read nicely.

    And it doesn’t happen automagically.
  • You might notice that the “Hash” returns a hash ref but not a hash.
    This is required because the it’s a reference to a tied hash. So the magic
    under the table, is to use tie.
  • Let’s write more crazy stuffs for this namespace.
  • Hey! It’s a Web-two-point-o-ey resource allocation exchange website!
  • Volunteer added good styles
  • Being a website that helps people in such a tragedy, the website should...
  • The whole points for such website
  • Now it’s over and the website basically shutdown...

    I looked back and thought..
  • They see a problem/requirement and they uses the technologies they familiar with.
  • Heroku is so convenient and cheap for RoR deployment. This makes me, as a Perl programmer for year, really jealous.
  • So by the end of this talk, I conclude..
  • Learning From Ruby (Yapc Asia)

    1. 1. Learning from Ruby Kang-min Liu <gugod@gugod.org> YAPC::Asia::2009
    2. 2. Gugod->meta • • http://handlino.com/ Kang-min Liu gugod • http://gugod.org • http://search.cpan.org/ ~gugod/ • http://twitter.com/gugod • gugod@gugod.org
    3. 3. Learning from Ruby
    4. 4. Learning from Ruby
    5. 5. Ruby is Lovely (The programming language)
    6. 6. Perl 6 Logo
    7. 7. Language
    8. 8. rubyisms.pm by Simon Cozens
    9. 9. rubyisms.pm • Class • new • self • super • yield
    10. 10. Perl ~~ Ruby
    11. 11. Ruby
    12. 12. self
    13. 13. class Foo def bar self.baz end def self.bar self.baz end end
    14. 14. class Foo def bar object self.baz end def self.bar self.baz end end
    15. 15. class Foo def bar object self.baz end Class def self.bar self.baz end end
    16. 16. class Foo def bar baz end def self.bar baz end end
    17. 17. block syntax
    18. 18. def foo(arg1, arg2) yield(a, b) end foo(arg1, arg2) do |block_arg1, block_arg2| ... end
    19. 19. co-routine
    20. 20. callback
    21. 21. call me after you done that.
    22. 22. div(id => "content") do h1(id => "title") { "OHAI" } p(class => "block") { "Cheeze?" } end
    23. 23. app do alert(42) end
    24. 24. Good Built-in Classes
    25. 25. Range
    26. 26. Lazy
    27. 27. irb> 1..2**100 => 1..1267650600228229401496703205376 irb> (1..2**100).class => Range
    28. 28. (1..2**100).each do |i| puts 42 break if i >= 5 end
    29. 29. autobox
    30. 30. no Semicolon
    31. 31. Rubyish.pm It’s all sugar, honey.
    32. 32. Rubyish.pm • Kernel, Object, Module, Class • Array, Hash, String, TrueClass, NilClass, Enumerable • Syntax • def, nil, true, false
    33. 33. class Foo { }
    34. 34. def foo { # $self; }
    35. 35. def foo($arg1, $arg2) { }
    36. 36. see also: MooseX::Declare
    37. 37. my $array = Array[0..3]; if ($array->any { $_ > 2 }) { ... }
    38. 38. # Rubyish::Array $arr = Array[1..10]; # Rubyish::Hash $hash = Hash{ a => 42, b => "stuff" }; # Rubyish::String $str = String("FooBar");
    39. 39. ref: Class::Builtin
    40. 40. boolean • true – TrueClass • false - FalseClass • nli - NilClass
    41. 41. sub true() { Rubyish::TrueClass->new }
    42. 42. ref: boolean.pm and Acme::Boolean
    43. 43. Hash is lazy too
    44. 44. fib = Hash.new { |fib, k| fib[k] = fib[k-1] + fib[k-2] } fib[0] = 0 fib[1] = 1 puts fib[10] # => 55
    45. 45. Functional programming -ish
    46. 46. Memoize sub fib { my $n = shift; return $n if $n < 2; fib($n-1) + fib($n-2); } memoize('fib');
    47. 47. Lazy stuffs • Scalar::Lazy • Variable::Lazy • Scalar::Defer • Data::Thunk
    48. 48. Lazy stuffs • Tie::Array::Lazy • Tie::Array::Lazier • Hash + Lazy = ?
    49. 49. Hash::Lazy
    50. 50. Hash::Lazy my $fib = Hash { my ($h, $k)= @_; return $h->{$k-1} + $h->{$k-2} }; $fib->{0} = 0; $fib->{1} = 1; say $fib->{10}; # 55
    51. 51. Hash::Lazy • export “Hash” as the constructor • Hash::Lazy::Tie
    52. 52. Learnt • I hate doing tie • Spiffy constructors can be good read • Hash • Rubyish.pm: Array, Hash, String • Class::Builtin: OO
    53. 53. PerlX::MethodCall WithBlock Still more syntax sugar
    54. 54. Schwartzian Transform Ruby (0..10) .map { |i| i * 13 } .map { |i| i % 17 } .sort { |a,b| a <=> b }
    55. 55. Schwartzian Transform Perl5 sort { $a <=> $b } map { $_ % 17 } map { $_ * 13 } (0..10)
    56. 56. Schwartzian Transform autobox Perl5, with [0..10] ->map(sub { $_ * 13 }) ->map(sub { $_ % 17 }) ->sort(sub { $a <=> $b })
    57. 57. Schwartzian Transform Perl5, extended! [0..10]->map { $_ * 13 }->map { $_ % 17 }->sort { my ($a,$b) = @_; $a <=> $b };
    58. 58. Schwartzian Transform Perl5, extended! [0..10] ->map { $_ * 13 } ->map { $_ % 17 } ->sort { $_[0] <=> $_[1] }
    59. 59. PerlX :: MethodCallWithBlo ck
    60. 60. ref • B::OPCheck • lineseq, pushmark, const • B::Hooks::EndOfScope • Devel::Declare
    61. 61. ref • opcode.pl, opcode.h • B::Concise • B::Deparse
    62. 62. PerlX
    63. 63. Community
    64. 64. Last month
    65. 65. Sh*t happened
    66. 66. Taiwan
    67. 67. Morakot
    68. 68.
    69. 69. $WebSite.com
    70. 70. xdite
    71. 71.
    72. 72. DisasterTW.com
    73. 73. DisasterTW.com • Forum • Initial Coding: 1 hour*woman.
    74. 74. 1 day later
    75. 75. DisasterTW.com • Web 2.0 / Social-ish • Multi-Function • Mash-able
    76. 76. DisasterTW.com ✖ • Web 2.0 / Social-ish • Multi-Function • Mash-able
    77. 77. DisasterTW.com • Online ASAP • Robust (peak: 400k PV/day) • Well-known
    78. 78. DisasterTW.com • Rapid Prototyping • Cloud Computing • SEO / Social Network Friendly
    79. 79. DisasterTW.com • Ruby on Rails • Heroku + Panther CDN • Good HTML / Plurk button
    80. 80. DisasterTW.com • 6 heroku instances • Thousands of tweets about #morakot • Total Cost: ~50 USD
    81. 81. People are awesome
    82. 82. Tools are awesome
    83. 83. Problem →
    84. 84. What if it was a Jifty site?
    85. 85. @obra can haz dancing
    86. 86. @obra can haz dancing, naked
    87. 87. DisasterTW.com • Rapid Prototyping • Cloud Computing • SEO / Social Network Friendly
    88. 88. DisasterTW.com • Jifty • Cloud Computing • Good HTML / Plurk button
    89. 89. Heroku for Perl?
    90. 90.
    91. 91. Ruby is Lovely
    92. 92. Ruby is a respectful friend of Perl
    93. 93. Ruby programmers are respectful friends of Perl programmers
    94. 94. Thanks for Listening

    ×