Perl: Difference between revisions

From Rixort Wiki
Jump to navigation Jump to search
No edit summary
Line 17: Line 17:


  use English;
  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>.

Revision as of 16:26, 5 September 2019

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].