Modern Perl

9,422 views
9,165 views

Published on

A brief introduction to modern Perl programming tools that I gave at the OpenTech conference in Spetember 2010.

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
9,422
On SlideShare
0
From Embeds
0
Number of Embeds
518
Actions
Shares
0
Downloads
137
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Modern Perl

  1. 1. Modern Perl
  2. 2. Fifteen years of Perl development
  3. 3. In twenty minutes
  4. 4. 1995
  5. 5. Everyone uses Perl to build dynamic web sites
  6. 6. Nasty CGI scripts
  7. 7. Web technology has moved on since then
  8. 8. Perl technology has moved on since then
  9. 9. Perceptions of Perl are stuck in the mid 90s
  10. 10. Hi! We're the Perl community and we suck at marketing
  11. 11. Perl has all the facilities you would expect in a modern dynamic language
  12. 12. Fully! Buzzword! Compliant!
  13. 13. A note on version numbers
  14. 14. Perl 5 is the current version of Perl
  15. 15. Specifically 5.12.1
  16. 16. Specifically 5.12.
  17. 17. Specifically 5.12.2
  18. 18. Perl 6 is still in development
  19. 19. (If you want to know more about Perl 6 then ask me later)
  20. 20. Perl 5 is still thriving
  21. 21. Some powerful Perl tools
  22. 22. Template Toolkit
  23. 23. Templating engine
  24. 24. Powerful and flexible
  25. 25. Web and non-web
  26. 26. Separation of concerns
  27. 27. Dear [% name %], You owe me £[% debt %]. Please pay up by [% date %] or I'll send the boys round. Love Dave...
  28. 28. #!/usr/bin/perl use Template; my $tt = Template->new; my $data = { name => 'Joe Random', debt => 100, date => '18 September', }; $tt->process('template.tt', $data);
  29. 29. Use with objects
  30. 30. [% FOREACH debt IN debts %] Dear [% debt.name %], You owe me £[% debt.amount %]. Please pay up by [% debt.date %] or I'll send the boys round. Love Dave... [% END %]
  31. 31. #!/usr/bin/perl use Template; use Debt; my $tt = Template->new; my @debts = Debt->find_all; $tt->process('template.tt', { debts => @debts});
  32. 32. http://tt2.org/
  33. 33. ORM
  34. 34. We all hate SQL
  35. 35. DBIx::Class
  36. 36. Builds on DBI
  37. 37. DBIx::Class::Schema::Loader
  38. 38. $ dbicdump MyClass 'dbi:mysql:<db>;<hostname>' <user> <password> Dumping manual schema for MyClass to directory . ... Schema dump completed.
  39. 39. <ul><li>Class for each table
  40. 40. Attribute for each column </li><ul><li>Type, mandatory/optional, auto-increment
  41. 41. Data type inflation </li></ul><li>Relationships </li></ul>
  42. 42. #!/usr/bin/perl use MyClass; my $sch = MyClass->connect('...'); my $objs = $sch->resultset('MyTable'); while ($objs->next) { print $_->name, “ ”; }
  43. 43. while (<FILE>) { my ($code, $name, $desc) = split; my $new_obj = $objs->create({ code => $code, name => $name, desc => $desc, }); print 'New object id: ', $new_obj->id, “ ”; }
  44. 44. <ul><li>Complex searching
  45. 45. Prefetching data
  46. 46. Many-to-many relationships
  47. 47. Database migrations
  48. 48. Replicated databases </li></ul>
  49. 49. http://dbix-class.org/
  50. 50. Moose
  51. 51. The Modern Object System for Perl 5
  52. 52. Perl 5's standard OO system looks a bit bolted on
  53. 53. (That's because it was bolted on)
  54. 54. Moose hides all that nastiness
  55. 55. Pretty syntactic sugar
  56. 56. Declarative syntax for attributes
  57. 57. package Debt; use Moose; has name => (isa => 'Str', is => 'rw', required => 1); has amount => (isa => 'Num', is => 'rw', required => 1); has date => (isa => 'DateTime', is => 'rw');
  58. 58. #!/usr/bin/perl use 5.010; use strict; use warnings; use Debt; my $debt = Debt->new({ name => 'Joe Random', amount => 100, }); say $debt->name, ' owes £', $debt->amount; # Add interest $debt->amount($debt->amount * 1.1); say $debt->name, ' owes £', $debt->amount;
  59. 59. use DateTime; # Set due date $debt->date(DateTime->now->add(days => 28)); say $debt->date; # Easier to read say $debt->date->strftime('%A %d %B %Y');
  60. 60. <ul><li>More attribute features </li><ul><li>Lazy build
  61. 61. Delegation
  62. 62. Defaults
  63. 63. Triggers </li></ul><li>Roles/Traits </li><ul><li>Like mixins or interfaces </li></ul></ul>
  64. 64. MooseX::*
  65. 65. MooSex::*
  66. 66. MooseX::*
  67. 67. http://moose.perl.org/
  68. 68. MVC
  69. 69. Catalyst
  70. 70. Builds on existing tools
  71. 71. Model is DBIx::Class
  72. 72. View is Template Toolkit
  73. 73. (These are just defaults)
  74. 74. Easy to get application framework running
  75. 75. $ catalyst.pl MyApp created &quot;MyApp&quot; created &quot;MyApp/script&quot; created &quot;MyApp/lib&quot; created &quot;MyApp/root&quot; created &quot;MyApp/root/static&quot; created &quot;MyApp/root/static/images&quot; created &quot;MyApp/t&quot; [ ... ] created &quot;MyApp/Makefile.PL&quot; created &quot;MyApp/script/myapp_cgi.pl&quot; created &quot;MyApp/script/myapp_fastcgi.pl&quot; created &quot;MyApp/script/myapp_server.pl&quot; created &quot;MyApp/script/myapp_test.pl&quot; created &quot;MyApp/script/myapp_create.pl&quot; Change to application directory and Run &quot;perl Makefile.PL&quot; to make sure your install is complete
  76. 76. $ cd MyApp $ script/myapp_server.pl [debug] Debug messages enabled [debug] Statistics enabled [debug] Loaded plugins: .----------------------------------------------------------------------------. | Catalyst::Plugin::ConfigLoader 0.27 | '----------------------------------------------------------------------------' [ ... lots of information ... ] [info] MyApp powered by Catalyst 5.80023 You can connect to your server at http://localhost:3000
  77. 78. Plugins to handle common requirements
  78. 79. Authentication/Authorisation
  79. 80. Session handling
  80. 81. CatalystX::*
  81. 82. Catalyst::Plugin::AUTOCRUD
  82. 83. http://catalystframework.org/
  83. 84. PSGI / Plack
  84. 85. PSGI is a specification
  85. 86. Like a super-charged CGI
  86. 87. And a lot like WSGI
  87. 88. Plack is a reference implementation
  88. 89. And a lot like Rack
  89. 90. Make it easy to move web apps
  90. 91. Different application technologies
  91. 92. Different web hosting technologies
  92. 93. Take a PSGI application and run it anywhere
  93. 94. PSGI app is a subroutine reference
  94. 95. # app.psgi my $app = sub { # clever stuff goes here };
  95. 96. Input comes from a hash passed to sub
  96. 97. # app.psgi my $app = sub { my $env = shift; # clever stuff goes here };
  97. 98. Sub returns a reference to an array
  98. 99. # app.psgi my $app = sub { my $env = shift; return [ 200, [ 'Content-type', 'text/plain' ], [ 'Hello world' ], ]; };
  99. 100. Plack distribution includes many tools
  100. 101. plackup - a simple PSGI web server
  101. 102. $ plackup app.psgi HTTP::Server::PSGI Accepting connections at http://localhost:5000/
  102. 103. Plack::Request Plack::Response
  103. 104. use Plack::Request; use Plack::Response; use Data::Dumper; my $app = sub { my $req = Plack::Request->new(shift); my $res = Plack::Response->new(200); $res->content_type('text/plain'); $res->body(Dumper $req); return $res->finalize; }
  104. 105. Plack::Middleware::* Plack::App::*
  105. 106. Most Perl web frameworks support Plack
  106. 107. http://plackperl.org/
  107. 108. Other Modern Perl tools
  108. 109. DateTime
  109. 110. TryCatch & autodie
  110. 111. Test::* & TAP::*
  111. 112. A lot has happened in the ten years you've been ignoring Perl
  112. 113. Your local Perl Mongers will be happy to tell you more
  113. 114. http://london.pm.org/ http://pm.org/
  114. 115. Dave Cross [email_address] @davorg @perlfoundation

×