Tutorial by Examples

Scalars are Perl's most basic data type. They're marked with the sigil $ and hold a single value of one of three types: a number (3, 42, 3.141, etc.) a string ('hi', "abc", etc.) a reference to a variable (see other examples). my $integer = 3; # number my $str...
Arrays store an ordered sequence of values. You can access the contents by index, or iterate over them. The values will stay in the order you filled them in. my @numbers_to_ten = (1,2,3,4,5,6,7,8,9,10); # More conveniently: (1..10) my @chars_of_hello = ('h','e','l','l','o'); my @word_list = ('Hel...
Hashes can be understood as lookup-tables. You can access its contents by specifiying a key for each of them. Keys must be strings. If they're not, they will be converted to strings. If you give the hash simply a known key, it will serve you its value. # Elements are in (key, value, key, value) se...
A reference is a scalar variable (one prefixed by $ ) which “refers to” some other data. my $value = "Hello"; my $reference = \$value; print $value; # => Hello print $reference; # => SCALAR(0x2683310) To get the referred-to data, you de-reference it. say ${$reference}...
Array References are scalars ($) which refer to Arrays. my @array = ("Hello"); # Creating array, assigning value from a list my $array_reference = \@array; These can be created more short-hand as follows: my $other_array_reference = ["Hello"]; Modifying / Using array ref...
Hash references are scalars which contain a pointer to the memory location containing the data of a hash. Because the scalar points directly to the hash itself, when it is passed to a subroutine, changes made to the hash are not local to the subroutine as with a regular hash, but instead are global...
A typeglob *foo holds references to the contents of global variables with that name: $foo, @foo, $foo, &foo, etc. You can access it like an hash and assign to manipulate the symbol tables directly (evil!). use v5.10; # necessary for say our $foo = "foo"; our $bar; say ref *foo{SCAL...
Perl has a number of sigils: $scalar = 1; # individual value @array = ( 1, 2, 3, 4, 5 ); # sequence of values %hash = ('it', 'ciao', 'en', 'hello', 'fr', 'salut'); # unordered key-value pairs &function('arguments'); # subroutine *typeglob; # symbol table entry These look like sigils, but...

Page 1 of 1