TITLE


Perl code fragments #2

From time to time, Perl syntax can confuse those who are new to it. It would be fair to say that on first glance, some Perl can look like some arcane language of an ancient tribe. Some of the syntax that can seem confusing is described briefly below.

@_

The special array @_ can be placed in a subroutine to be used to read the arguments passed to a subroutine. The @_ array is specifically designed to hold arguments passed to a subroutine.

sub special_array_test
{
$value1 = @_[0];
$value1 = @_[0];
print ($value1 + $value2)
}

The values from the @_ special array can also be retrieved by using the shift function as follows:

sub special_array_test
{
$value1 = shift @_;
$value1 = shift @_;
print ($value1 + $value2)
}

Since the shift function uses the @_ special array by default in a subroutine, the subroutine above could also be rewritten as:

sub special_array_test
{
$value1 = shift;
$value1 = shift;
print ($value1 + $value2)
}

->

The arrow operator or infix dereference operator, is based on the equivalent operator in C, and is used to dereference a reference

$hash{ceres} = asteroid;
$hash{mars} = planet;
$hash{sun} = star;
$hashref = \%hash;
print $hashref->{mars}

planet




www.cyberthinc.com