Section 2.8. Commas


2.8. Commas

Place a comma after every value in a multiline list.

Just as semicolons act as separators in a block of statements, commas act as separators in a list of values. That means that exactly the same arguments apply in favour of treating them as terminators instead.

Adding an extra trailing comma (which is perfectly legal in any Perl list) also makes it much easier to reorder the elements of the list. For example, it's much easier to convert:

      my @dwarves = (         'Happy',         'Sleepy',         'Dopey',         'Sneezy',         'Grumpy',         'Bashful',         'Doc',     ); 

to:

      my @dwarves = (         'Bashful',         'Doc',         'Dopey',         'Grumpy',         'Happy',         'Sleepy',         'Sneezy',     ); 

You can manually cut and paste lines or even feed the list contents through sort.

Without that trailing comma after 'Doc', reordering the list would introduce a bug:

     my @dwarves = (         'Bashful',         'Doc'         'Dopey',         'Grumpy',         'Happy',         'Sleepy',         'Sneezy',     );

Of course, that's a trivial mistake to find and fix, but why not adopt a coding style that eliminates the very possibility of such problems?



Perl Best Practices
Perl Best Practices
ISBN: 0596001738
EAN: 2147483647
Year: 2004
Pages: 350
Authors: Damian Conway

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net