Perl: Difference between revisions

From Rixort Wiki
Jump to navigation Jump to search
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 9: Line 9:


Only remove one or more of the above if you '''really''' know what you are doing.
Only remove one or more of the above if you '''really''' know what you are doing.
It is also a good idea to define a minimum Perl version, e.g. to require Perl 5.14 or above:
use v5.14;
More readable names for common variables (e.g. <code>$/</code> can be referred to as <code>$INPUT_RECORD_SEPARATOR</code>) can be created with:
use English;
== Arrays and lists ==
* Although used interchangeably, technically a list is the collection of elements whereas an array is the variable that points to the list.
* <code>$#array</code> contains the last element index of the array. Since arrays are zero-indexed, the size of the array is <code>$#array + 1</code>.
* Negative array indices start from the end of the list, so the last element can be accessed as <code>$array[-1]</code>.
* An empty list is represented by <code>()</code>, and an empty array can be initialised as <code>my @array = ();</code>
* List ranges can be generated using <code>..</code>, e.g. </code>(1..10)</code> will include the numbers from 1 to 10.
* The <code>qw</code> function allows a list to be generated from elements separated by whitespace, without the need for quotes or commas, e.g. <code>@array = qw(a b c d)</code>.
* Array items can either be a scalar value, or a reference to another list. If you use an array in a list (e.g. <code>@list = (@array)</code>), it will be expanded to a scalar value rather than being a reference to the array.
== Performance ==
* Scoping hurts performance (but probably improves readability and keeps variables scoped)
== Talks ==
* [https://www.youtube.com/watch?v=bSd8FwdIALY Perl Optimisization Tidbits] - some surprising results (Perl 5.22, so may no longer be relevant)
* [https://www.youtube.com/watch?v=L98dUbVN2Mw Modern Approaches to Ancient Perls]

Latest revision as of 11:07, 12 April 2023

Robust scripts

All scripts should start with the following:

use strict;
use warnings;
use utf8;
use autodie;

Only remove one or more of the above if you really know what you are doing.

It is also a good idea to define a minimum Perl version, e.g. to require Perl 5.14 or above:

use v5.14;

More readable names for common variables (e.g. $/ can be referred to as $INPUT_RECORD_SEPARATOR) can be created with:

use English;

Arrays and lists

  • Although used interchangeably, technically a list is the collection of elements whereas an array is the variable that points to the list.
  • $#array contains the last element index of the array. Since arrays are zero-indexed, the size of the array is $#array + 1.
  • Negative array indices start from the end of the list, so the last element can be accessed as $array[-1].
  • An empty list is represented by (), and an empty array can be initialised as my @array = ();
  • List ranges can be generated using .., e.g. (1..10) will include the numbers from 1 to 10.
  • The qw function allows a list to be generated from elements separated by whitespace, without the need for quotes or commas, e.g. @array = qw(a b c d).
  • Array items can either be a scalar value, or a reference to another list. If you use an array in a list (e.g. @list = (@array)), it will be expanded to a scalar value rather than being a reference to the array.

Performance

  • Scoping hurts performance (but probably improves readability and keeps variables scoped)

Talks