@fruits = qw(apple orange pear); $refFruits = \@fruits
$fruit = @$refFruits[1];
%profile1 = (name => "Alice",
email => "alice\@home.com");
%profile2 = (name => "Bob",
email => "bob\@home.com");
$directory{"Alice"} = \%profile1;
$directory{"Bob"} = \%profile2;
$refProfile = $directory{"Alice"};
%profile = %$refProfile;
$email = $profile{"email"};
$email = ${ $directory{"Alice"} }{"email"};
$email = $directory{"Alice"}->{"email"};
$refArray->[0] = "Bob"; # Array element
$refHash->{"key"} = "value"; # Hash element
$refCode->(1, 2, 3); # Subroutine call
$refArray = [ element-1, element-2, ... ];
$refHash = {
key-1 => "value-1",
key-2 => "value-2",
...
};
$refArray = [ 1, 2, 3 ];
$refHash = {
"Adam" => "Eve",
"Clyde" => "Bonnie",
};
$directory = (
"Alice" => {
name => "Alice",
email => "alice\@home.com"
},
"Bob" => {
name => "Bob",
email => "bob\@home.com"
},
);
$matrix = ( [1, 2, 3], [0, 4, 5], [0, 0, 6], );
$matrix[0]->[0] == 1 $matrix[1]->[1] == 4 $matrix[2]->[2] == 6
$number
== != < > <= >=
eq ne lt gt le ge
&& AND || OR ! NOT
(($name eq "Lucy") && ($age >= 18))
@fruits = qw(apple orange pear);
%prices = (
apple => 0.99,
orange => 1.29,
pear => 0.69
);
foreach $fruit (@fruits) {
print "<p>Price for $fruit is $$ $prices{$fruit}";
}
@fruits = qw(apple orange pear);
for ($i=0; $i<@fruits; $i++) {
print "<p>Fruit for sale: $fruits[$i]\n";
}
sub header {
my $title = shift @_;
print << END_OF_HEADER;
Content-Type: text/html
<html>
<head>
<title>$title</title>
</head>
<body>
END_OF_HEADER;
}
my ($x, $y, ...) = @_;
my $title = shift @_;
sub square {
my $x = shift @_;
$x * $x;
}
sub grade {
my $marks = shift @_;
if ($marks >= 90) {
return "A+";
} elsif ($marks >= 85) {
return "A";
} elsif ($marks >= 80) {
return "A-";
} elsif ...
# other cases
}
}
$result = square($value); print "Square of $value is $result\n"; $grade = &grade(95); print "Final grade: $grade\n";
1;
sub Header {
my $title = shift(@_);
print "Content-Type: text/html\n\n";
print "<html><head><title>";
print "$title";
print "</title></head><body>\n";
}
sub Footer {
print "\n</body></html>";
}
1; # OK
require "header_footer.lib";
Header(); print << END_OF_BODY; <h1 align="center">Order confirmation</h1> Thank you for your order. Your confirmation number is $orderId. END_OF_BODY Footer();