Brackets in Perl

For beginners, the usage of different types of brackets might be confusing. When to use curly brackets '{}', round brackets '()' or square brackets '[]'? After reading this post, you will be pretty clear.

Round brackets '()'


1. initialize array or hash
my %hash = ('key' => 'value' );
my @array = (1,4,5,6);

Curly brackets '{}'


1. access to hash values.
my %hash = ('key' => 'value' );
print $hash{'key'};

2. anonymous hash
my $hash = { 'key' => 'value' };

Square brackets '[]'


1. access elements of arrays.
my @array = (1,4,5,6);
print $array[3];

No comments:

Post a Comment