perl best practices

| No Comments | No TrackBacks
Jacinta Richardson first referred to Damin Conways 2005 book. The obvious things she said was use warnings, use strict. Have a coding standard. 

She said to use 5.8.9, 5.10.1 or 5.12.2. She said 'use v5.10.1;', to let you use all the new features. She mentioned say(). Also defined or, which is //.
 $price //= $item->cost
So if the price is not defined it gets set to the item cost.

She mentioned state variables. "state $images_allowed = 100;". So the variable hangs around after the function call, and when you return to the function it is still initialised.

She mentioned the given when construct, which is like a C switch statement, but more powerful. You can do a foreach when loop as well, which is pretty cool. The when looks work because of smart matching, ~~. Smart match does magic comparison, which handles if you are comparing to an array, or a string, or a hash, etc.

From 5.10.1+ you have autodie in perl. So you just 'use autodie;', and system calls automatically throw exceptions. To catch exceptions you no longer use an eval block. Now you can 'use Try::Tiny;', and then we can use try{} catch{}; blocks. Within modules you shouldn't call 'die', because the error message isn't so helpful to the user. So instead 'use carp;', and then do 'croak "we broke";'.

She highligthed using perlbrew to easily get versions of perl installed into your home directory. There is local::lib to make it easy to test local versions of modules. It sets up perl environment to use local module. cpanm can be used to get install into your homedir the perl modules.

module-starter is a great way to get started writing a module. It has templates to create module stubs, stubs of documentation, initial tests, a Makefile.

Jacinta talked about Smart Commenting. Then she talked about tests, and said that it is really easy, if you 'use Test::Most tests => 4;'. She talked about using perltidy to make your code more pretty. Can use perlcritic to review your code (in ways that are easy to automate). It refers to Damines book when it explains why things. Like saying you should use strict.

She reminded the audience to use CPAN. She highlighted some modules to remember, like List::Util shuffle. She said to use an ORM rather than SQL statements directly. DBIx::Class and some other thing were the favourite ORMS. There are some frameworks, such as Catalyst and Moose.

Moose makes Object Orientation much better than non Moose OO in perl.

Method::Signatures allow you to do
 func nom ($biscuit, $cookie) {}
 method get ($key) {
  return $self->{ $key };
 }

Path::Class abstracts away interacting with files and directories, which also makes the code more portable to other operating systems.

Regular expressions are a language within perl. She said to use \A and \Z meta characters. So use \A instead of ^ to match the start of a string, in case /m gets added to the regexp later on. Similarly \Z instead of $. You can use alternate delimiters. You don't have to write '/\//\//', you can write 'm{//}'. Try to use qr{} fragments, if that make sense, qr stands for quoted regular expression. Since v5.10.0+ you can do named captures from regular expressions.

She says you should avoid regexp that have already been written. Use Regexp::Common.

Perl 5.10.0 added grammars to the language, and sometimes you should use a grammar instead of a regular expression. This is in Regexp::Grammars.

She referred to Michael Schwerns talk on Monday, about perl5i. SHe said if you missed it you should go and watch the video. From her example it appears the perl5i automatically does lots of use statements for the features Jacinta has been talking about.

No TrackBacks

TrackBack URL: http://geoff-blog.cromp.id.au/cgi-bin/movabletype/mt-tb.cgi/128

Leave a comment

About this Entry

This page contains a single entry by Geoff Crompton published on January 27, 2011 4:43 PM.

bat phone was the previous entry in this blog.

extracting requirements from flame wars is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Categories

Pages

Powered by Movable Type 4.23-en