Variables, Misc

Array of arrays

  • A array of arrays is essentially an array of references to arrays (and should be treated as such)
  • The same applies to an array of hashes, a hash of arrays, and a hash of hashes

Variable prefixes

$    # scalar, or an element of an array or hash
@ # array
% # hash

Special variables

Default variable

$_   # used in many cases where no variable is explicitly stated

Command-line arguments

@ARGV      # command-line arguments for the script; 0-based
$#ARGV # index number of the last argument
$ARGV[0] # the first command-line argument
{$0} # name of the program being executed

Subroutine arguments

@_      # the arguments for the subroutine; 0-based
$_[0] # the first argument for the subroutine
my ($arg1, $arg2) = @_;

Environment variables

%ENV

Variable scope

my

  • Defines a local variable within a subroutine or a source file
  • Usage of my is almost always preferable to using local
  • If the variable is declared outside any subroutine, it has file scope
my $scalar1 = 2;

our

  • Declares a package variable
our $var1;

Resources URL: 
notes/perl/resources
Sources URL: 
notes/perl/sources

See Also