Introduction to Perl - Day 2
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • wa wa
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
24,127
On Slideshare
24,050
From Embeds
77
Number of Embeds
5

Actions

Shares
Downloads
1,521
Comments
1
Likes
15

Embeds 77

http://www.slideshare.net 73
http://sigma 1
http://www.lmodules.com 1
http://translate.googleusercontent.com 1
http://www.linkedin.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Introduction to Perl Day 2 An Introduction to Perl Programming Dave Cross Magnum Solutions Ltd [email_address]
  • 2. What We Will Cover
    • Types of variable
    • 3. Strict and warnings
    • 4. References
    • 5. Sorting
    • 6. Reusable Code
    • 7. Object Orientation
  • 8. What We Will Cover
  • 13. Schedule
    • 09:30 – Begin
    • 14. 11:00 – Coffee break (30 mins)
    • 15. 13:00 – Lunch (90 mins)
    • 16. 14:30 – Begin
    • 17. 16:00 – Coffee break (30 mins)
    • 18. 18:00 – End
  • 19. Resources
    • Slides available on-line
      • http://mag-sol.com/train/public/2009/yapc
    • Also see Slideshare
      • http://www.slideshare.net/davorg/slideshows
    • Get Satisfaction
      • http://getsatisfaction.com/magnum
  • 20. Types of Variable
  • 21. Types of Variable
    • Perl variables are of two types
    • 22. Important to know the difference
    • 23. Lexical variables are created with my
    • 24. Package variables are created by our
    • 25. Lexical variables are associated with a code block
    • 26. Package variables are associated with a package
  • 27. Lexical Variables
    • Created with my
    • 28. my ($doctor, @timelords, %home_planets);
    • 29. Live in a pad (associated with a block of code)
      • Piece of code delimited by braces
      • 30. Source file
    • Only visible within enclosing block
    • 31. "Lexical" because the scope is defined purely by the text
  • 32. Packages
    • All Perl code is associated with a package
    • 33. A new package is created with package
      • package MyPackage;
    • Think of it as a namespace
    • 34. Used to avoid name clashes with libraries
    • 35. Default package is called main
  • 36. Package Variables
    • Live in a package's symbol table
    • 37. Can be referred to using a fully qualified name
      • $main::doctor
      • 38. @Gallifrey::timelords
    • Package name not required within own package
    • 39. Can be seen from anywhere in the package (or anywhere at all when fully qualified)
  • 40. Declaring Package Vars
    • Can be predeclared with our
    • 41. our ($doctor, @timelords, %home_planet);
    • 42. Or (in older Perls) with use vars
    • 43. use vars qw($doctor @timelords %home_planet);
  • 44. Lexical or Package
    • When to use lexical variables or package variables?
    • 45. Simple answer
      • Always use lexical variables
    • More complete answer
      • Always use lexical variables
      • 46. Except for a tiny number of cases
    • http://perl.plover.com/local.html
  • 47. local
    • You might see code that uses local
    • 48. local $variable;
    • 49. This doesn't do what you think it does
    • 50. Badly named function
    • 51. Doesn't create local variables
    • 52. Creates a local copy of a package variable
    • 53. Can be useful
      • In a small number of cases
  • 54. local Example
    • $/ is a package variable
    • 55. It defines the record separator
    • 56. You might want to change it
    • 57. Always localise changes
    • 58. { local $/ = “ ”; while (<FILE> ) { ... } }
  • 59. Strict and Warnings
  • 60. Coding Safety Net
    • Perl can be a very loose programming language
    • 61. Two features can minimise the dangers
    • 62. use strict / use warnings
    • 63. A good habit to get into
    • 64. No serious Perl programmer codes without them
  • 65. use strict
    • Controls three things
    • 66. use strict 'refs' – no symbolic references
    • 67. use strict 'subs' – no barewords
    • 68. use strict 'vars' – no undeclared variables
    • 69. use strict – turn on all three at once
    • 70. turn them off (carefully) with no strict
  • 71. use strict 'refs'
    • Prevents symbolic references
    • 72. aka &quot;using a variable as another variable's name&quot;
    • 73. $what = 'dalek'; $$what = 'Karn'; # sets $dalek to 'Karn'
    • 74. What if 'dalek' came from user input?
    • 75. People often think this is a cool feature
    • 76. It isn't
  • 77. use strict 'refs' (cont)
    • Better to use a hash
    • 78. $what = 'dalek'; $alien{$what} = 'Karn';
    • 79. Self contained namespace
    • 80. Less chance of clashes
    • 81. More information (e.g. all keys)
  • 82. use strict 'subs'
    • No barewords
    • 83. Bareword is a word with no other interpretation
    • 84. e.g. word without $, @, %, &
    • 85. Treated as a function call or a quoted string
    • 86. $dalek = Karn;
    • 87. May clash with future reserved words
  • 88. use strict 'vars'
    • Forces predeclaration of variable names
    • 89. Prevents typos
    • 90. Less like BASIC - more like Ada
    • 91. Thinking about scope is good
  • 92. use warnings
    • Warns against dubious programming habits
    • 93. Some typical warnings
      • Variables used only once
      • 94. Using undefined variables
      • 95. Writing to read-only file handles
      • 96. And many more...
  • 97. Allowing Warnings
    • Sometimes it's too much work to make code warnings clean
    • 98. Turn off use warnings locally
    • 99. Turn off specific warnings
    • 100. { no warnings 'deprecated'; # dodgy code ... }
    • 101. See perldoc perllexwarn
  • 102. References
  • 103. Introducing References
    • A reference is a bit like pointer in languages like C and Pascal (but better)
    • 104. A reference is a unique way to refer to a variable.
    • 105. A reference can always fit into a scalar variable
    • 106. A reference looks like SCALAR(0x20026730)
  • 107. Creating References
    • Put in front of a variable name
      • $scalar_ref = $scalar;
      • 108. $array_ref = @array;
      • 109. $hash_ref = hash;
    • Can now treat it just like any other scalar
      • $var = $scalar_ref;
      • 110. $refs[0] = $array_ref;
      • 111. $another_ref = $refs[0];
  • 112. Creating References
    • [ LIST ] creates anonymous array and returns a reference
    • 113. $aref = [ 'this', 'is', 'a', 'list']; $aref2 = [ @array ];
    • 114. { LIST } creates anonymous hash and returns a reference
    • 115. $href = { 1 => 'one', 2 => 'two' }; $href = { %hash };
  • 116. Creating References
    • @arr = (1, 2, 3, 4); $aref1 = @arr; $aref2 = [ @arr ]; print &quot;$aref1 $aref2 &quot;;
    • 117. Output ARRAY(0x20026800) ARRAY(0x2002bc00)
    • 118. Second method creates a copy of the array
  • 119. Using Array References
    • Use {$aref} to get back an array that you have a reference to
    • 120. Whole array
    • 121. @array = @{$aref};
    • 122. @rev = reverse @{$aref};
    • 123. Single elements
    • 124. $elem = ${$aref}[0];
    • 125. ${$aref}[0] = 'foo';
  • 126. Using Hash References
    • Use {$href} to get back a hash that you have a reference to
    • 127. Whole hash
    • 128. %hash = %{$href};
    • 129. @keys = keys %{$href};
    • 130. Single elements
    • 131. $elem = ${$href}{key};
    • 132. ${$href}{key} = 'foo';
  • 133. Using References
    • Use arrow ( -> ) to access elements of arrays or hashes
    • 134. Instead of ${$aref}[0] you can use $aref->[0]
    • 135. Instead of ${$href}{key} you can use $href->{key}
  • 136. Using References
    • You can find out what a reference is referring to using ref
    • 137. $aref = [ 1, 2, 3 ]; print ref $aref; # prints ARRAY
    • 138. $href = { 1 => 'one', 2 => 'two' }; print ref $href; # prints HASH
  • 139. Why Use References?
    • Parameter passing
    • 140. Complex data structures
  • 141. Parameter Passing
    • What does this do?
    • 142. @arr1 = (1, 2, 3); @arr2 = (4, 5, 6); check_size(@arr1, @arr2); sub check_size { my (@a1, @a2) = @_; print @a1 == @a2 ? 'Yes' : 'No'; }
  • 143. Why Doesn't It Work?
    • my (@a1, @a2) = @_;
    • 144. Arrays are combined in @_
    • 145. All elements end up in @a1
    • 146. How do we fix it?
    • 147. Pass references to the arrays
  • 148. Another Attempt
    • @arr1 = (1, 2, 3); @arr2 = (4, 5, 6); check_size(@arr1, @arr2); sub check_size { my ($a1, $a2) = @_; print @$a1 == @$a2 ? 'Yes' : 'No'; }
  • 149. Complex Data Structures
    • Another good use for references
    • 150. Try to create a 2-D array
    • 151. @arr_2d = ((1, 2, 3), (4, 5, 6), (7, 8, 9));
    • 152. @arr_2d contains (1, 2, 3, 4, 5, 6, 7, 8, 9)
    • 153. This is known a array flattening
  • 154. Complex Data Structures
    • 2D Array using references
    • 155. @arr_2d = ([1, 2, 3], [4, 5, 6], [7, 8, 9]);
    • 156. But how do you access individual elements?
    • 157. $arr_2d[1] is ref to array (4, 5, 6)
    • 158. $arr_2d[1]->[1] is element 5
  • 159. Complex Data Structures
    • Another 2D Array
    • 160. $arr_2d = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
    • 161. $arr_2d->[1] is ref to array (4, 5, 6)
    • 162. $arr_2d->[1]->[1] is element 5
    • 163. Can omit intermediate arrows
    • 164. $arr_2d->[1][1]
  • 165. More Data Structures
    • Imagine the following data file
    • 166. Jones,Martha,UNIT Harkness,Jack,Torchwood Smith,Sarah Jane,Journalist
    • 167. What would be a good data structure?
    • 168. Hash for each record
    • 169. Array of records
    • 170. Array of hashes
  • 171. More Data Structures
    • Building an array of hashes
    • 172. my @records; my @cols = ('s_name', 'f_name', 'job'); while (<FILE>) { chomp; my %rec; @rec{@cols} = split /,/; push @records, rec; }
  • 173. Using an Array of Hashes foreach (@records) { print &quot;$_->{f_name} &quot;, &quot;$_->{s_name} &quot;. &quot;is a $_->{job} &quot;; }
  • 174. Complex Data Structures
    • Many more possibilities
      • Hash of hashes
      • 175. Hash of lists
      • 176. Multiple levels (list of hash of hash, etc.)
    • Lots of examples in “perldoc perldsc” (the data structures cookbook)
  • 177. Sorting
  • 178. Sorting
    • Perl has a sort function that takes a list and sorts it
    • 179. @sorted = sort @array;
    • 180. Note that it does not sort the list in place
    • 181. @array = sort @array;
  • 182. Sort Order
    • The default sort order is ASCII
    • 183. @chars = sort 'e', 'b', 'a', 'd', 'c'; # @chars has ('a', 'b', 'c', 'd', 'e')
    • 184. This can sometimes give strange results
    • 185. @chars = sort 'E', 'b', 'a', 'D', 'c'; # @chars has ('D', 'E', 'a', 'b', 'c')
    • 186. @nums = sort 1 .. 10; # @nums has (1, 10, 2, 3, 4, # 5, 6, 7, 8, 9)
  • 187. Sorting Blocks
    • Can add a &quot;sorting block&quot; to customise sort order
    • 188. @nums = sort { $a <=> $b } 1 .. 10;
    • 189. Perl puts two of the values from the list into $a and $b
    • 190. Block compares values and returns -1, 0 or 1
    • 191. <=> does this for numbers ( cmp for strings)
  • 192. Sort Examples
    • Other simple sort examples
    • 193. sort { $b cmp $a } @words
    • 194. sort { lc $a cmp lc $b } @words
    • 195. sort { substr($a, 4) cmp substr($b, 4) } @lines
  • 196. Sorting Subroutines
    • Can also use a subroutine name in place of a code block
    • 197. @words = sort dictionary @words; sub dictionary { # Don't change $a and $b my ($A, $B) = ($a, $b); $A =~ s/W+//g; $B =~ s/W+//g; $A cmp $B; }
  • 198. Sorting Names
    • my @names = ('Rose Tyler', 'Martha Jones', 'Donna Noble', 'Amy Pond'); @names = sort sort_names @names;
    • 199. Need to write sort_names so that it sorts on surname and then forename.
  • 200. Sorting Names (cont)
    • sub sort_names { my @a = split /s/, $a; my @b = split /s/, $b; return $a[1] cmp $b[1] or $a[0] cmp $b[1]; }
  • 201. More Complex Sorts
    • sub sort_names { my @a = split /s/, $a; my @b = split /s/, $b; return $a[1] cmp $b[1] or $a[0] cmp $b[0]; }
    • 202. Can be inefficient on large amounts of data
    • 203. Multiple splits on the same data
  • 204. More Efficient Sorts
    • Split each row only once
    • 205. @split = map { [ split ] } @names;
    • 206. Do the comparison
    • 207. @sort = sort { $a->[1] cmp $b->[1] or $a->[0] cmp $b->[0] } @split;
    • 208. Join the data together
    • 209. @names = map { join ' ', @$_ } @sort;
  • 210. Put It All Together
    • Can rewrite this as
    • 211. @names = map { join ' ', @$_ } sort { $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] } map { [ split ] } @names;
    • 212. All functions work on the output from the previous function in the chain
  • 213. Schwartzian Transform
    • @data_out = map { $_->[1] } sort { $a->[0] cmp $a->[0] } map { [func($_), $_] } @data_in;
    • 214. Old Lisp trick
    • 215. Named after Randal Schwartz
  • 216. Reusable Code
  • 217. Why Write Modules?
    • Code reuse
    • 218. Prevent reinventing the wheel
    • 219. Easier to share across projects
    • 220. Better design, more generic
  • 221. Basic Module
    • use strict; use warnings; package MyModule; use Exporter; our @ISA = ('Exporter'); our @EXPORT = ('my_sub'); sub my_sub { print &quot;This is my_sub &quot;; } 1;
  • 222. Using MyModule.pm
    • use MyModule; # my_sub is now available # for use within your # program my_sub(); # Prints &quot;This is my_sub()&quot;
  • 223. Explaining MyModule.pm
    • Much of MyModule.pm is concerned with exporting subroutine names
    • 224. Subroutine full name
      • MyModule::my_sub()
    • Exporting abbreviates that
      • my_sub()
  • 225. Packages Revisited
    • Every subroutine lives in a package
    • 226. The default package is main
    • 227. New packages are introduced with the package keyword
    • 228. A subroutine's full name is package::name
    • 229. Package name can be omitted from within same package
    • 230. Like family names
  • 231. Using Exporter
    • The module Exporter.pm handles the export of subroutine (and variable) names
    • 232. Exporter.pm defines a subroutine called import
    • 233. import is automatically called whenever a module is used
    • 234. import puts references to our subroutines into our caller's symbol table
  • 235. How Exporter Works
    • How does MyModule use Exporter's import subroutine?
    • 236. We make use of inheritance
    • 237. Inheritance is defined using the @ISA array
    • 238. If we call a subroutine that doesn't exist in our module, then the modules in @ISA are also checked
    • 239. Therefore Exporter::import is called
  • 240. Exporting Symbols
    • How does import know which subroutines to export?
    • 241. Exports are defined in @EXPORT or @EXPORT_OK
    • 242. Automatic exports are defined in @EXPORT
    • 243. Optional exports are defined in @EXPORT_OK
  • 244. Exporting Symbol Sets
    • You can define sets of exports in %EXPORT_TAGS
    • 245. Key is set name
    • 246. Value is reference to an array of names
    • 247. our %EXPORT_TAGS = (advanced => [ qw( my_sub my_other_sub ) ]; use MyModule qw(:advanced); my_sub(); my_other_sub();
  • 248. Why Use @EXPORT_OK?
    • Give your users the choice of which subroutines to import
    • 249. Less chances of name clashes
    • 250. Use @EXPORT_OK in preference to @EXPORT
    • 251. Document the exported names and sets
  • 252. Exporting Variables
    • You can also export variables
    • 253. @EXPORT_OK = qw($scalar, @array, %hash);
    • 254. Can be part of export sets
    • 255. Any variables you export must be package variables
  • 256. Writing Modules The Easy Way
    • A lot of module code is similar
    • 257. Don't write the boilerplate yourself
    • 258. Copy from an existing module
    • 259. Or look at Module::Starter
  • 260. Object Orientation
  • 261. OO Programming
    • Traditional programming has subroutines acting on methods
    • 262. OO inverts this
    • 263. Classes contain methods which define their actions
    • 264. Objects are instances of classes
    • 265. Perl has an OO system bolted on
    • 266. Best of both worlds
  • 267. Object Oriented Perl
    • An Object is just a module that obeys certain extra rules
    • 268. Three rules of Perl objects
      • A Class is a package
      • 269. An Object is a reference (usually to a hash)
      • 270. A Method is a subroutine
    • bless tells a reference what kind of object it is
  • 271. A Simple Object
    • package MyObject; sub new { my $class = shift; my $name = shift; my $self = { name => $name }; return bless $self, $class; }
  • 272. A Simple Object (cont)
    • sub get_name { my $self = shift; return $self->{name}; } sub set_name { my $self = shift; $self->{name} = shift; } 1;
  • 273. Using MyObject.pm
    • use MyObject; my $obj = MyObject->new('Dave'); print $obj->get_name; # prints 'Dave' $obj->set_name('David'); print $obj->get_name; # prints 'David'
  • 274. Moose
    • Easier OO Perl
    • 275. Moose is on CPAN
    • 276. Based on Perl 6 OO
    • 277. Well worth investigating
  • 278. Moose Example
    • package MyModule; use Moose; has name => (is => 'rw', isa => 'Str', required => 1);
    • 279. 1;
  • 280. Further Information
    • perldoc perlboot
    • 281. perldoc perltoot
    • 282. perldoc perlobj
    • 283. perldoc perlbot
    • 284. perldoc Moose (if it is installed)
    • 285. Object Oriented Perl (Conway)
  • 286. Testing
  • 287. Testing
    • Never program without a safety net
    • 288. Does your code do what it is supposed to do?
    • 289. Will your code continue to do what it is supposed to do?
    • 290. Write unit tests
    • 291. Run those tests all the time
  • 292. When to Run Tests
    • As often as possible
    • 293. Before you add a feature
    • 294. After you have added a feature
    • 295. Before checking in code
    • 296. Before releasing code
    • 297. Constantly, automaticall y
  • 298. Testing in Perl
    • Perl makes it easy to write test suites
    • 299. A lot of work in this area over the last eight years
    • 300. Test::Simple and Test::More included in Perl distribution
    • 301. Many more testing modules on CPAN
  • 302. Simple Test Program
    • use Test::More tests => 4; BEGIN { use_ok('My::Object'); } ok(my $obj = My::Object->new); isa_ok($obj, 'My::Object'); $obj->set_foo('Foo'); is($obj->get_foo, 'Foo');
  • 303. Simple Test Output
    • $ prove -v test.t test.... 1..4 ok 1 - use My::Object; ok 2 ok 3 - The object isa My::Object ok 4 ok All tests successful. Files=1, Tests=4, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.05 cusr 0.00 csys = 0.07 CPU) Result: PASS
  • 304. Adding Test Names
    • use Test::More tests => 4; BEGIN { use_ok('My::Object'); } ok(my $obj = My::Object->new, 'Got an object'); isa_ok($obj, 'My::Object'); $obj->set_foo('Foo'); is($obj->get_foo, 'Foo', 'The foo is &quot;Foo&quot;');
  • 305. Output With Names
    • $ prove -v test2.t test2.... 1..4 ok 1 - use My::Object; ok 2 - got an object ok 3 - The object isa My::Object ok 4 - The foo is &quot;Foo&quot; ok All tests successful. Files=1, Tests=4, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.05 cusr 0.00 csys = 0.07 CPU) Result: PASS
  • 306. Using prove
    • prove is a command line tool for running tests
    • 307. Runs given tests using Test::Harness
    • 308. Comes with the Perl distribution
    • 309. Command line options
  • 313. Test Anything Protocol
    • Perl tests have been spitting out “ok 1” and not “ok 2” for years
    • 314. Now this ad-hoc format has a definition and a name
    • 315. The Test Anything Protocol (TAP)
    • 316. See Test::Harness::TAP (documentation module) and TAP::Parser
  • 317. TAP Output
    • More possibilities for test output
      • TAP::Harness::Color
      • 318. Test::TAP::HTMLMatrix
    • Make sense of your test results
  • 319. More Testing Modules
    • Dozens of testing modules on CPAN
    • 320. Some of my favourites
    • 321. Test::File
    • 322. Test::Exception, Test::Warn
    • 323. Test::Differences
    • 324. Test::XML (includes Test::XML::XPath)
  • 325. Writing Test Modules
    • These test modules all work together
    • 326. Built using Test::Builder
    • 327. Ensures that test modules all use the same framework
    • 328. Use it as the basis of your own Test::* modules
    • 329. Who tests the testers?
    • 330. Test your Test::Builder test modules with Test::Builder::Tester
  • 331. How Good Are Your Tests?
    • How much of your code is exercised by your tests?
    • 332. Devel::Cover can help you to find out
    • 333. Deep internal magic
    • 334. Draws pretty charts
      • HARNESS_PERL_SWITCHES= -MDevel::Cover make test
      • 335. cover
  • 336. Devel::Cover Output
  • 337. Devel::Cover Output
  • 338. Devel::Cover Output
  • 339. Alternative Test Paradigms
    • Not everyone likes the Perl testing framework
    • 340. Other frameworks are available
    • 341. Test::Class
      • xUnit style framework
    • Test::FIT
      • Framework for Interactive Testing
      • 342. http://fit.c2.com
  • 343. More Information
    • Perl Testing: A Developer's Notebook (Ian Langworth & chromatic)
    • 344. perldoc Test::Tutorial
    • 345. perldoc Test::Simple
    • 346. perldoc Test::More
    • 347. perldoc Test::Builder
    • 348. etc...
  • 349. Dates and Times
  • 350. Dates & Times
    • Perl has built-in functions to handle dates and times
    • 351. Flexible but confusing
    • 352. time – seconds since 1st Jan 1970
    • 353. localtime – convert to human-readable
    • 354. timelocal (in Time::Local) – inverse of localtime
    • 355. strftime (in POSIX) – formatting dates and times
  • 356. Dates & Times on CPAN
    • Look to CPAN for a better answer
    • 357. Dozens of date/time modules on CPAN
    • 358. Date::Manip is almost never what you want
    • 359. Date::Calc, Date::Parse, Class::Date, Date::Simple, etc
    • 360. Which one do you choose?
  • 361. Perl DateTime Project
    • http://datetime.perl.org/
    • 362. &quot;The DateTime family of modules present a unified way to handle dates and times in Perl&quot;
    • 363. &quot;unified&quot; is good
    • 364. Dozens of modules that work together in a consistent fashion
  • 365. Using DateTime
    • use DateTime; my $dt = DateTime->now; say $dt; # 2009-08-02T15:06:07 say $dt->dmy, &quot; &quot;; # 2009-08-02 say $dt->hms, &quot; &quot;; # 15:06:07
  • 366. Using DateTime
    • use DateTime; my $dt = DateTime->new(year => 2009, month => 8, day => 2); say $dt->ymd('/'), &quot; &quot;; # 2009/08/02 say $dt->month; # 8 say $dt->month_name; # August
  • 367. Arithmetic
    • A DateTime object is a point in time
    • 368. For date arithmetic you need a duration
    • 369. Number of years, weeks, days, etc
  • 370. Arithmetic
    • use DateTime; my $dt = DateTime->new(year => 2009, month => 8, day => 2); my $two_weeks = DateTime::Duration->new(weeks => 2); $dt += $two_weeks; say $dt; # 2009-02-16T00:00:00
  • 371. Formatting Output
    • use DateTime; my $dt = DateTime->new(year => 2009, month => 2, day => 26); say $dt->strftime('%A, %d %B %Y'); # Tuesday, 26 February 2009
    • 372. Control input format with DateTime::Format::Strptime
  • 373. Parsing & Formatting
    • Ready made parsers and formatters for popular date and time formats
    • 374. DateTime::Format::HTTP
    • 375. DateTime::Format::MySQL
    • 376. DateTime::Format::Excel
    • 377. DateTime::Format::Baby
      • the big hand is on...
  • 378. Alternative Calendars
    • Handling non-standard calendars
    • 379. DateTime::Calendar::Julian
    • 380. DateTime::Calendar::Hebrew
    • 381. DateTime::Calendar::Mayan
    • 382. DateTime::Fiction::JRRTolkien::Shire
  • 383. Calendar Examples
    • use DateTime::Calendar::Mayan; my $dt = DateTime::Calendar::Mayan->now; say $dt->date; # 12.19.16.9.15
    • 384. use DateTime::Fiction::JRRTolkien::Shire my $dt = DateTime::Fiction::JRRTolkien::Shire->now; say $dt->on_date; # Trewsday 1 Wedmath 7473
  • 385. Templates
  • 386. Templates
    • Many people use templates to produce web pages
    • 387. Advantages are well known
    • 388. Standard look and feel (static/dynamic)
    • 389. Reusable components
    • 390. Separation of code logic from display logic
    • 391. Different skill-sets (HTML vs Perl)
  • 392. Non-Web Templates
    • The same advantages apply to non-web areas
    • 393. Reports
    • 394. Business documents
    • 395. Configuration files
    • 396. Anywhere you produce output
  • 397. DIY Templating
    • Must be easy - so many people do it
    • 398. See perlfaq4
    • 399. “How can I expand variables in text strings?”
  • 400. DIY Templating
    • $text = 'this has a $foo in it and a $bar'; %user_defs = ( foo => 23, bar => 19, ); $text =~ s/$(w+)/$user_defs{$1}/g;
    • 401. Don't do that
  • 402. Templating Options
    • Dozens of template modules on CPAN
    • 403. Text::Template, HTML::Template, Mason, Template Toolkit
    • 404. Many, many more
    • 405. Questions to consider
      • HTML only?
      • 406. Template language
    • I choose the Template Toolkit
  • 407. Template Toolkit
    • http://tt2.org/
    • 408. Very powerful
    • 409. Both web and non-web
    • 410. Simple template language
    • 411. Plugins give access to much of CPAN
    • 412. Can use Perl code if you want
      • But don't do that
  • 413. Good Book Too!
  • 414. The Template Equation
    • Data + Template = Output
    • 415. Data + Alternative Template = Alternative Output
    • 416. Different views of the same data
    • 417. Only the template changes
  • 418. Simple TT Example
    • use Template; use My::Object; my ($id, $format) = @ARGV; $format ||= 'html'; my $obj = My::Object->new($id) or die; my $tt = Template->new; $tt->process(&quot;$format.tt&quot;, { obj => $obj }, &quot;$id.$format&quot;) or die $tt->error;
  • 419. html.tt
    • <html> <head> <title>[% obj.name %]</title> </head> <body> <h1>[% obj.name %]<h1> <p><img src=“[% obj.img %]” /><br /> [% obj.desc %]</p> <ul> [% FOREACH child IN obj.children -%] <li>[% child.name %]</li> [% END %] </body> </html>
  • 420. text.tt
    • [% obj.name | upper %] Image: [% obj.img %] [% obj.desc %] [% FOREACH child IN obj.children -%] * [% child.name %] [% END %]
  • 421. Adding New Formats
    • No new code required
    • 422. Just add new output template
    • 423. Perl programmer need not be involved
  • 424. Equation Revisited
    • Data + Template = Output
      • Template Toolkit
    • Template + Output = Data
      • Template::Extract
    • Data + Output = Template
      • Template::Generate
  • 425. Databases
  • 426. Databases
    • A large proportion of applications need to talk to databases
    • 427. Perl has tools to make this as simple as possible
    • 428. DBI is the basis for all modern Perl database access
    • 429. You should be using DBI
      • or something based on DBI
  • 430. How DBI Works
    • Program uses DBI.pm
    • 431. Create a connection to a particular type of database
    • 432. DBD module gets loaded
    • 433. DBD translates from DBI API to database specific calls
    • 434. DBD translates returned data into Perl data stuctures
  • 435. Connecting to a DB
    • use DBI; my $dbh = DBI->connect( &quot;dbi:mysql:$some_stuff&quot;, $user, $pass );
    • 436. “mysql” is the name of the DBD
      • DBD::mysql
    • In theory you can port a program to another database
    • 437. Just change the connection line
  • 438. Selecting Data
    • Prepare the SQL statement
    • 439. my $sth = $dbh->prepare( 'select name, genre from artist' );
    • 440. my $sth = $dbh->prepare( &quot;select title, from song where artist = '$id'&quot;);
    • 441. Check return values (syntax errors)
  • 442. Selecting Data (cont)
    • Execute the statement
    • 443. $sth->execute
    • 444. Still need to check for errors
  • 445. Selecting Data (cont)
    • Fetch the returned data
    • 446. while (my @row = $sth->fetchrow_array){ print &quot;@row &quot;; }
    • 447. Fields are returned in the same order as they are defined in the query
  • 448. Other Select Functions
    • Other fetch methods are available:
      • fetchrow_arrayref
      • 449. fetchrow_hashref (keys are column names)
      • 450. fetchall_arrayref
      • 451. fetch (alias for fetchrow_arrayref)
    • Many more added each week
  • 452. Some Caveats
    • If you're using a fetch method that returns an array
      • Never use &quot;select *&quot;
      • 453. For (hopefully) obvious reasons
    • If you're using a fetch method that returns a hash
      • Ensure all your columns have (unique) names
      • 454. For (hopefully) obvious reasons
  • 455. Insert, Update & Delete
    • Statements that don't return data can be executed the same way
    • 456. my $sql = &quot;update table1 set col1 = '$val' where id_col = $id&quot;; my $sth = $dbh->prepare($sql); $sth->execute;
    • 457. But there's a shortcut
    • 458. $rows = $dbh->do($sql);
  • 459. Multiple Insertions
    • while (<FILE>) { chomp; my @data = split; my $sql = &quot;insert into tab values ($data[0], $data[1], $data[2]&quot;); $dbh->do($sql); }
    • 460. Recompiles the SQL every time
    • 461. Very inefficient
  • 462. Binding Data
    • Prepare statement once, use many times
    • 463. my $sql = &quot;insert into tab values (?, ?, ?)&quot;; my $sth = $dbh->prepare($sql); while (<FILE>) { my @data = split; bind_param(1, $data[0]); bind_param(2, $data[1]); bind_param(3, $data[2]); $sth->execute; }
    • 464. Bonus - binding handles quoting for you
  • 465. Binding Data (cont)
    • Even easier – extra parameters to execute
    • 466. my $sql = &quot;insert into tab values (?, ?, ?)&quot;; my $sth = $dbh->prepare($sql); while (<FILE>) { chomp; my @data = split; $sth->execute(@data); }
  • 467. Unnamed Placeholders
    • Having unnamed placeholders can get confusing
    • 468. my $sql = 'insert into big_table values( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
    • 469. Good chance of getting the variables in the wrong order
    • 470. By the way - there's a basic maintainability error in that SQL
  • 471. Bind By Name
    • my $sql = 'insert into big_table (id, code, name, addr, email, url, ... ) values (:id, :code, :name, :addr, :email, :url, ... ); my $sth = $sql->prepare($sql); $sth->bind_param(':id', $id); $sth->bind_param(':code', $code); # etc $sth->execute;
  • 472. Even Easier Binding
    • Store your data in a hash
    • 473. my %data = (id => 42, code => 'H2G2', ... ); # and later... foreach my $col (keys %data) { $sth->bind_param(&quot;:$col&quot;, $data{$col}; }
  • 474. Downsides
    • Many DBDs don't support it
    • 475. Which is a bit of a bugger
    • 476. Oracle does
    • 477. So does PostgreSQL (tho' the docs discourage its use)
    • 478. Check your DBD documentation
    • 479. Email your friendly neighbourhood DBD author
  • 480. Some Tips
    • Make your life as easy as possible
    • 481. Don't hard-code connection data
      • Config file, command line options, environment variables
    • Send all of your data access through one function
    • 482. Store SQL queries externally and reference them by name
    • 483. Use named bind parameters if you can
  • 484. Sample Code
    • my $dbh; sub run_sql { my ($sql_statement, %args) = @_; my $sql = get_sql($sql_statement); $dbh = get_dbh() unless $dbh; my $sth = $dbh->prepare($sql); foreach my $col (keys %args) { $sth->bind_param(&quot;:$col&quot;, $args{$col}); } return $sth->execute; }
  • 485. Not Writing SQL
    • Writing SQL is boring
    • 486. It's often similar
      • Select the id and name from this table
      • 487. Select all the details of this row
      • 488. Select something about related tables
      • 489. Update this row with these values
      • 490. Insert a new record with these values
      • 491. Delete this record
    • Must be a better way
  • 492. Object Relational Mapping
    • Mapping database relations into objects
    • 493. Tables (relations) map onto classes
    • 494. Rows (tuples) map onto objects
    • 495. Columns (attributes) map onto attributes
    • 496. Don't write SQL
  • 497. Replacing SQL
    • Instead of
    • 498. SELECT * FROM my_table WHERE my_id = 10
    • 499. and then dealing with the prepare/execute/fetch code
  • 500. Replacing SQL
    • We can write
    • 501. use My::Object; # warning! not a real orm my $obj = My::Object->retrieve(10) $obj->name('A New Name'); $obj->save;
    • 502. Or something similar
  • 503. ORM on CPAN
  • 509. Further Information
    • perldoc DBI
    • 510. perldoc DBD::*
    • perldoc DBIx::Class
  • 513. Further Information
  • 514. Perl Websites
    • Perl Home Page
      • http://www.perl.org
    • CPAN
      • http://www.cpan.org
      • 515. http://search.cpan.org
    • Perl Mongers (Perl User Groups)‏
      • http://www.pm.org
      • 516. http://london.pm.org
  • 517. Perl Websites
    • use.perl – Perl News & Journals
      • http://use.perl.org/
    • Perl Monks – Perl Help and Advice
      • http://perlmonks.org/
    • Perl Documentation Online
      • http://perldoc.perl.org/
  • 518. Perl Conferences
    • The Perl Conference (part of the Open Source Convention)‏
      • July 20-24 2009, San Jose, California
      • 519. http://conferences.oreilly.com/oscon
    • Yet Another Perl Conference
      • August 3-5 2009, Lisbon, Portugal
      • 520. http://www.yapceurope.org
  • 521. Perl Conferences
  • 525. Perl Workshops
    • One-day grassroots conferences
    • 526. UK, Germany, Israel, Pittsburgh, Nordic, Netherlands, France, Belgium, Russia, Minnesota, Austria
    • 527. Perl Review Calendar
      • www.theperlreview.com/community_calendar
  • 528. Perl Mailing Lists
  • 535. Perl Books
    • Books for learning Perl
      • Learning Perl (5th ed - June 2008) Schwartz, Phoenix & foy (O'Reilly)‏
      • 536. Intermediate Perl Schwartz, foy & Phoenix (O'Reilly)‏
      • 537. Beginning Perl Cozens (Wrox) http://www.perl.org/books/beginning-perl/
  • 538. Perl Books
    • Books you should have access to
      • Programming Perl (3rd edition) Wall, Christiansen & Orwant (O'Reilly)‏
      • 539. The Perl Cookbook (2nd edition) Christiansen & Torkington (O'Reilly)‏
      • 540. Perl Best Practices Conway (O'Reilly)‏
      • 541. Perl in a Nutshell Siever, Spainhour & Patwardhan (O'Reilly)‏
  • 542. Perl Books
    • Books you should probably look at
      • Mastering Regular Expressions Friedl (O'Reilly)‏
      • 543. Data Munging with Perl Cross (Manning)‏
      • 544. Advanced Perl Programming Cozens (O'Reilly)‏
      • 545. Perl Medic Scott (Addison Wesley)
  • 546. Perl Books
    • Specialised Perl books
      • Object Oriented Perl Conway (Manning)‏
      • 547. Programming the Perl DBI Descartes & Bunce (O'Reilly)‏
      • 548. Writing CGI Applications with Perl Meltzer & Michelski (Addison Wesley)‏
      • 549. Practical mod_perl Bekman & Cholet (O'Reilly)‏
      • 550. Perl Template Toolkit Chamberlain, Cross & Wardley
  • 551. Perl Magazines
    • The Perl Review
      • http://www.theperlreview.com
  • 552. That's All Folks
    • Any Questions?