Quick perl syntax walkthrough

datatype and variables
Perl maintains every variable type in a separate namespace. So you can, without fear of conflict, use the same name for a scalar variable, an array, or a hash. This means that $foo and @foo are two different variables.

array

@names = ("John Paul", "Lisa", "Kumar");
print "$names[0]\n";
@array = (1, 2, 'Hello');
# populate using qw operator
@array = qw/This is an array/;

hash
%data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);
print "$data{'John Paul'}";

special literal
print "File name ". __FILE__ . "\n";
print "Line Number " . __LINE__ ."\n";
print "Package " . __PACKAGE__ ."\n";

perldebug
open debugger on command line iwth perl -de1

private variables in sub-routine with 'my' keyword

tempoary value to give global variable via local keyword

reference to variables and subroutine
$scalarref = \$foo;
$arrayref = [1, 2, ['a', 'b', 'c']];
$hashref = {
'Adam'  => 'Eve',
'Clyde' => 'Bonnie',
};

# ref to anonounous sub-routine
$coderef = sub { print "Boink!\n" };

# reference to sub-routine
$cref = \&PrintHash;

dereference with $, @, % for scalar, array and hash
use & to dereference subroutine

评论

此博客中的热门博文

AVR Tutorial - Input / Output

AVR ADC Analog to Digital Converter

Introduction to REST