Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Aug 262010
 

Hash reference in Perl

Answer:

Hash reference is something like a pointer to a hash data structure in Perl

Example:

my %hash = (
        "a" => "1",
        "b" => "2",
);

my $hash_ref = \%hash;

$hash{"a"} = "A";

print $hash_ref->{"a"}; # print out "A" as change in  %hash  also reflected in its reference

Aug 212010
 

A simple hash in Perl

Answer:

Hash is a dictionary like data structure in Perl. It allows you to direct access to a particular element inside a hash.

Example:

use strict;
use warnings;

my %hash = (
        "a" => "1",
        "b" => "2",
);

print $hash{"b"}; # print "2"
Aug 162010
 

A simple array in Perl

Answer:

Array is a list like data structure in Perl. It allows you to access to the elements inside the array sequentially.

Example:

use strict;
use warnings;

my @a = (1, 2, 3);

print $a[2]; # print '3', since Perl array is zero-indexed
Jul 242010
 

Multiline string in Perl

Answer:

You can either use the q operators.

my $s = q#''''"
test
"""'
#;
print $s;

Or qq operators if you need interpolation.

my $d = "test";
my $s = qq#''''"
$d
"""'
#;

print $s;