Advertisement

Introduction to Perl - Day 2

Owner / Senior Developer at Magnum Solutions Ltd
Aug. 3, 2009
Advertisement

More Related Content

Advertisement
Advertisement

Introduction to Perl - Day 2

  1. Introduction to Perl Day 2 An Introduction to Perl Programming Dave Cross Magnum Solutions Ltd [email_address]
  2. Strict and warnings
  3. References
  4. Sorting
  5. Reusable Code
  6. Object Orientation
  7. Dates and Times
  8. Templates
  9. Databases
  10. Further Information
  11. 11:00 – Coffee break (30 mins)
  12. 13:00 – Lunch (90 mins)
  13. 14:30 – Begin
  14. 16:00 – Coffee break (30 mins)
  15. 18:00 – End
  16. Types of Variable
  17. Important to know the difference
  18. Lexical variables are created with my
  19. Package variables are created by our
  20. Lexical variables are associated with a code block
  21. Package variables are associated with a package
  22. my ($doctor, @timelords, %home_planets);
  23. "Lexical" because the scope is defined purely by the text
  24. Used to avoid name clashes with libraries
  25. Default package is called main
  26. Can be seen from anywhere in the package (or anywhere at all when fully qualified)
  27. our ($doctor, @timelords, %home_planet);
  28. Or (in older Perls) with use vars
  29. use vars qw($doctor @timelords %home_planet);
  30. local $variable;
  31. This doesn't do what you think it does
  32. Badly named function
  33. Doesn't create local variables
  34. Creates a local copy of a package variable
  35. It defines the record separator
  36. You might want to change it
  37. Always localise changes
  38. { local $/ = “”; while (<FILE> ) { ... } }
  39. Strict and Warnings
  40. Two features can minimise the dangers
  41. use strict / use warnings
  42. A good habit to get into
  43. No serious Perl programmer codes without them
  44. use strict 'refs' – no symbolic references
  45. use strict 'subs' – no barewords
  46. use strict 'vars' – no undeclared variables
  47. use strict – turn on all three at once
  48. turn them off (carefully) with no strict
  49. aka &quot;using a variable as another variable's name&quot;
  50. $what = 'dalek'; $$what = 'Karn'; # sets $dalek to 'Karn'
  51. What if 'dalek' came from user input?
  52. People often think this is a cool feature
  53. It isn't
  54. $what = 'dalek'; $alien{$what} = 'Karn';
  55. Self contained namespace
  56. Less chance of clashes
  57. More information (e.g. all keys)
  58. Bareword is a word with no other interpretation
  59. e.g. word without $, @, %, &
  60. Treated as a function call or a quoted string
  61. $dalek = Karn;
  62. May clash with future reserved words
  63. Prevents typos
  64. Less like BASIC - more like Ada
  65. Thinking about scope is good
  66. Using undefined variables
  67. Writing to read-only file handles
  68. And many more...
  69. Turn off use warnings locally
  70. Turn off specific warnings
  71. { no warnings 'deprecated'; # dodgy code ... }
  72. See perldoc perllexwarn
  73. References
  74. A reference is a unique way to refer to a variable.
  75. A reference can always fit into a scalar variable
  76. A reference looks like SCALAR(0x20026730)
  77. $array_ref = array;
  78. $refs[0] = $array_ref;
  79. $another_ref = $refs[0];
  80. $aref = [ 'this', 'is', 'a', 'list']; $aref2 = [ @array ];
  81. { LIST } creates anonymous hash and returns a reference
  82. $href = { 1 => 'one', 2 => 'two' }; $href = { %hash };
  83. Output ARRAY(0x20026800) ARRAY(0x2002bc00)
  84. Second method creates a copy of the array
  85. Whole array
  86. @array = @{$aref};
  87. @rev = reverse @{$aref};
  88. Single elements
  89. $elem = ${$aref}[0];
  90. ${$aref}[0] = 'foo';
  91. Whole hash
  92. %hash = %{$href};
  93. @keys = keys %{$href};
  94. Single elements
  95. $elem = ${$href}{key};
  96. ${$href}{key} = 'foo';
  97. Instead of ${$aref}[0] you can use $aref->[0]
  98. Instead of ${$href}{key} you can use $href->{key}
  99. $aref = [ 1, 2, 3 ]; print ref $aref; # prints ARRAY
  100. $href = { 1 => 'one', 2 => 'two' }; print ref $href; # prints HASH
  101. Complex data structures
  102. @arr1 = (1, 2, 3); @arr2 = (4, 5, 6); check_size(@arr1, @arr2); sub check_size { my (@a1, @a2) = @_; print @a1 == @a2 ? 'Yes' : 'No'; }
  103. Arrays are combined in @_
  104. All elements end up in @a1
  105. How do we fix it?
  106. Pass references to the arrays
  107. Try to create a 2-D array
  108. @arr_2d = ((1, 2, 3), (4, 5, 6), (7, 8, 9));
  109. @arr_2d contains (1, 2, 3, 4, 5, 6, 7, 8, 9)
  110. This is known a array flattening
  111. @arr_2d = ([1, 2, 3], [4, 5, 6], [7, 8, 9]);
  112. But how do you access individual elements?
  113. $arr_2d[1] is ref to array (4, 5, 6)
  114. $arr_2d[1]->[1] is element 5
  115. $arr_2d = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
  116. $arr_2d->[1] is ref to array (4, 5, 6)
  117. $arr_2d->[1]->[1] is element 5
  118. Can omit intermediate arrows
  119. $arr_2d->[1][1]
  120. Jones,Martha,UNIT Harkness,Jack,Torchwood Smith,Sarah Jane,Journalist
  121. What would be a good data structure?
  122. Hash for each record
  123. Array of records
  124. Array of hashes
  125. my @records; my @cols = ('s_name', 'f_name', 'job'); while (<FILE>) { chomp; my %rec; @rec{@cols} = split /,/; push @records, rec; }
  126. Using an Array of Hashes foreach (@records) { print &quot;$_->{f_name} &quot;, &quot;$_->{s_name} &quot;. &quot;is a $_->{job}&quot;; }
  127. Hash of lists
  128. Sorting
  129. @sorted = sort @array;
  130. Note that it does not sort the list in place
  131. @array = sort @array;
  132. @chars = sort 'e', 'b', 'a', 'd', 'c'; # @chars has ('a', 'b', 'c', 'd', 'e')
  133. This can sometimes give strange results
  134. @chars = sort 'E', 'b', 'a', 'D', 'c'; # @chars has ('D', 'E', 'a', 'b', 'c')
  135. @nums = sort 1 .. 10; # @nums has (1, 10, 2, 3, 4, # 5, 6, 7, 8, 9)
  136. @nums = sort { $a <=> $b } 1 .. 10;
  137. Perl puts two of the values from the list into $a and $b
  138. Block compares values and returns -1, 0 or 1
  139. <=> does this for numbers ( cmp for strings)
  140. sort { $b cmp $a } @words
  141. sort { lc $a cmp lc $b } @words
  142. sort { substr($a, 4) cmp substr($b, 4) } @lines
  143. @words = sort dictionary @words; sub dictionary { # Don't change $a and $b my ($A, $B) = ($a, $b); $A =~ s/+//g; $B =~ s/+//g; $A cmp $B; }
  144. Need to write sort_names so that it sorts on surname and then forename.
  145. Can be inefficient on large amounts of data
  146. Multiple splits on the same data
  147. @split = map { [ split ] } @names;
  148. Do the comparison
  149. @sort = sort { $a->[1] cmp $b->[1] or $a->[0] cmp $b->[0] } @split;
  150. Join the data together
  151. @names = map { join ' ', @$_ } @sort;
  152. @names = map { join ' ', @$_ } sort { $a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] } map { [ split ] } @names;
  153. All functions work on the output from the previous function in the chain
  154. Old Lisp trick
  155. Named after Randal Schwartz
  156. Reusable Code
  157. Prevent reinventing the wheel
  158. Easier to share across projects
  159. Better design, more generic
  160. The default package is main
  161. New packages are introduced with the package keyword
  162. A subroutine's full name is package::name
  163. Package name can be omitted from within same package
  164. Like family names
  165. Exporter.pm defines a subroutine called import
  166. import is automatically called whenever a module is used
  167. import puts references to our subroutines into our caller's symbol table
  168. We make use of inheritance
  169. Inheritance is defined using the @ISA array
  170. If we call a subroutine that doesn't exist in our module, then the modules in @ISA are also checked
  171. Therefore Exporter::import is called
  172. Exports are defined in @EXPORT or @EXPORT_OK
  173. Automatic exports are defined in @EXPORT
  174. Optional exports are defined in @EXPORT_OK
  175. Key is set name
  176. Value is reference to an array of names
  177. our %EXPORT_TAGS = (advanced => [ qw( my_sub my_other_sub ) ]; use MyModule qw(:advanced); my_sub(); my_other_sub();
  178. Less chances of name clashes
  179. Use @EXPORT_OK in preference to @EXPORT
  180. Document the exported names and sets
  181. @EXPORT_OK = qw($scalar, @array, %hash);
  182. Can be part of export sets
  183. Any variables you export must be package variables
  184. Don't write the boilerplate yourself
  185. Copy from an existing module
  186. Or look at Module::Starter
  187. Object Orientation
  188. OO inverts this
  189. Classes contain methods which define their actions
  190. Objects are instances of classes
  191. Perl has an OO system bolted on
  192. Best of both worlds
  193. An Object is a reference (usually to a hash)
  194. Moose is on CPAN
  195. Based on Perl 6 OO
  196. Well worth investigating
  197. 1;
  198. perldoc perltoot
  199. perldoc perlobj
  200. perldoc perlbot
  201. perldoc Moose (if it is installed)
  202. Object Oriented Perl (Conway)
  203. Testing
  204. Does your code do what it is supposed to do?
  205. Will your code continue to do what it is supposed to do?
  206. Write unit tests
  207. Run those tests all the time
  208. Before you add a feature
  209. After you have added a feature
  210. Before checking in code
  211. Before releasing code
  212. Constantly, automaticall y
  213. A lot of work in this area over the last eight years
  214. Test::Simple and Test::More included in Perl distribution
  215. Many more testing modules on CPAN
  216. Runs given tests using Test::Harness
  217. Comes with the Perl distribution
  218. -r recurse
  219. -s shuffle tests
  220. Many more
  221. Now this ad-hoc format has a definition and a name
  222. The Test Anything Protocol (TAP)
  223. See Test::Harness::TAP (documentation module) and TAP::Parser
  224. Some of my favourites
  225. Test::File
  226. Test::Exception, Test::Warn
  227. Test::Differences
  228. Test::XML (includes Test::XML::XPath)
  229. Built using Test::Builder
  230. Ensures that test modules all use the same framework
  231. Use it as the basis of your own Test::* modules
  232. Who tests the testers?
  233. Test your Test::Builder test modules with Test::Builder::Tester
  234. Devel::Cover can help you to find out
  235. Deep internal magic
  236. cover
  237. Devel::Cover Output
  238. Devel::Cover Output
  239. Devel::Cover Output
  240. Other frameworks are available
  241. http://fit.c2.com
  242. perldoc Test::Tutorial
  243. perldoc Test::Simple
  244. perldoc Test::More
  245. perldoc Test::Builder
  246. etc...
  247. Dates and Times
  248. Flexible but confusing
  249. time – seconds since 1st Jan 1970
  250. localtime – convert to human-readable
  251. timelocal (in Time::Local) – inverse of localtime
  252. strftime (in POSIX) – formatting dates and times
  253. Dozens of date/time modules on CPAN
  254. Date::Manip is almost never what you want
  255. Date::Calc, Date::Parse, Class::Date, Date::Simple, etc
  256. Which one do you choose?
  257. &quot;The DateTime family of modules present a unified way to handle dates and times in Perl&quot;
  258. &quot;unified&quot; is good
  259. Dozens of modules that work together in a consistent fashion
  260. For date arithmetic you need a duration
  261. Number of years, weeks, days, etc
  262. Control input format with DateTime::Format::Strptime
  263. DateTime::Format::HTTP
  264. DateTime::Format::MySQL
  265. DateTime::Format::Excel
  266. DateTime::Calendar::Julian
  267. DateTime::Calendar::Hebrew
  268. DateTime::Calendar::Mayan
  269. DateTime::Fiction::JRRTolkien::Shire
  270. use DateTime::Fiction::JRRTolkien::Shire my $dt = DateTime::Fiction::JRRTolkien::Shire->now; say $dt->on_date; # Trewsday 1 Wedmath 7473
  271. Templates
  272. Advantages are well known
  273. Standard look and feel (static/dynamic)
  274. Reusable components
  275. Separation of code logic from display logic
  276. Different skill-sets (HTML vs Perl)
  277. Reports
  278. Business documents
  279. Configuration files
  280. Anywhere you produce output
  281. See perlfaq4
  282. “How can I expand variables in text strings?”
  283. Don't do that
  284. Text::Template, HTML::Template, Mason, Template Toolkit
  285. Many, many more
  286. Very powerful
  287. Both web and non-web
  288. Simple template language
  289. Plugins give access to much of CPAN
  290. Good Book Too!
  291. Data + Alternative Template = Alternative Output
  292. Different views of the same data
  293. Only the template changes
  294. Just add new output template
  295. Perl programmer need not be involved
  296. Databases
  297. Perl has tools to make this as simple as possible
  298. DBI is the basis for all modern Perl database access
  299. Create a connection to a particular type of database
  300. DBD module gets loaded
  301. DBD translates from DBI API to database specific calls
  302. DBD translates returned data into Perl data stuctures
  303. Just change the connection line
  304. my $sth = $dbh->prepare( 'select name, genre from artist' );
  305. my $sth = $dbh->prepare( &quot;select title, from song where artist = '$id'&quot;);
  306. Check return values (syntax errors)
  307. $sth->execute
  308. Still need to check for errors
  309. while (my @row = $sth->fetchrow_array){ print &quot;@row&quot;; }
  310. Fields are returned in the same order as they are defined in the query
  311. fetchrow_hashref (keys are column names)
  312. fetchall_arrayref
  313. For (hopefully) obvious reasons
  314. my $sql = &quot;update table1 set col1 = '$val' where id_col = $id&quot;; my $sth = $dbh->prepare($sql); $sth->execute;
  315. But there's a shortcut
  316. $rows = $dbh->do($sql);
  317. Recompiles the SQL every time
  318. Very inefficient
  319. 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; }
  320. Bonus - binding handles quoting for you
  321. my $sql = &quot;insert into tab values (?, ?, ?)&quot;; my $sth = $dbh->prepare($sql); while (<FILE>) { chomp; my @data = split; $sth->execute(@data); }
  322. my $sql = 'insert into big_table values( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
  323. Good chance of getting the variables in the wrong order
  324. By the way - there's a basic maintainability error in that SQL
  325. my %data = (id => 42, code => 'H2G2', ... ); # and later... foreach my $col (keys %data) { $sth->bind_param(&quot;:$col&quot;, $data{$col}; }
  326. Which is a bit of a bugger
  327. Oracle does
  328. So does PostgreSQL (tho' the docs discourage its use)
  329. Check your DBD documentation
  330. Email your friendly neighbourhood DBD author
  331. Store SQL queries externally and reference them by name
  332. Use named bind parameters if you can
  333. Select all the details of this row
  334. Select something about related tables
  335. Update this row with these values
  336. Insert a new record with these values
  337. Tables (relations) map onto classes
  338. Rows (tuples) map onto objects
  339. Columns (attributes) map onto attributes
  340. Don't write SQL
  341. SELECT * FROM my_table WHERE my_id = 10
  342. and then dealing with the prepare/execute/fetch code
  343. use My::Object; # warning! not a real orm my $obj = My::Object->retrieve(10) $obj->name('A New Name'); $obj->save;
  344. Or something similar
  345. Tangram
  346. Alzabo
  347. Class::DBI
  348. Highly recommended
  349. DBD::Oracle
  350. Further Information
  351. http://london.pm.org
  352. http://www.yapceurope.org
  353. Brazil
  354. Australia
  355. UK, Germany, Israel, Pittsburgh, Nordic, Netherlands, France, Belgium, Russia, Minnesota, Austria
  356. CGI
  357. DBI
  358. XML
  359. Beginners
  360. Advocacy
  361. Fun with Perl
  362. Intermediate Perl Schwartz, foy & Phoenix (O'Reilly)‏
  363. Beginning Perl Cozens (Wrox) http://www.perl.org/books/beginning-perl/
  364. The Perl Cookbook (2nd edition) Christiansen & Torkington (O'Reilly)‏
  365. Perl Best Practices Conway (O'Reilly)‏
  366. Perl in a Nutshell Siever, Spainhour & Patwardhan (O'Reilly)‏
  367. Data Munging with Perl Cross (Manning)‏
  368. Advanced Perl Programming Cozens (O'Reilly)‏
  369. Perl Medic Scott (Addison Wesley)
  370. Programming the Perl DBI Descartes & Bunce (O'Reilly)‏
  371. Writing CGI Applications with Perl Meltzer & Michelski (Addison Wesley)‏
  372. Practical mod_perl Bekman & Cholet (O'Reilly)‏
  373. Perl Template Toolkit Chamberlain, Cross & Wardley
Advertisement