$a = "x"; $b = "y";
($b,$a) = ($a,$b);
float
or int
or string
typestrue
or false
undef
for “undefined”constants
not provided by defaultpackage My::Module;
sub new {
my $self = {};
return bless($self);
}
sub uppercase {
my $self = shift();
my $string = shift();
return uc($string);
}
$scalar = 'hello';
@array = ('apple','banana','cherry');
%hash = (code => 2000, suburb => 'CBD');
$array[2]
— the third item in an array$hash{'postcode'}
— the value stored against 'postcode' in a hash==
and !=
eq
and ne
>
and <
gt
and lt
+
$price + $surcharge;
+
$prefix . $suffix;
2 + 2 = 22
situation like in Javascript.$_
which you could translate as “it”:foreach('apple','banana','cherry'){say $_}
$@
which you can translate as “them”:sub do_something{
foreach(@_){
say $_;
}
}
do_something('apple','banana','cherry');
localtime()
function returns the time$time = localtime();
say $time;
Sun Jun 20 10:39:15 2021
@time = localtime();
say "@time";
(15, 39, 10, 20, 5, 121, 0, 170, 0)
if($something){ do_something() }
unless($something){ do_something() }
if( ! $something ){ do_something() }
while()
, the until()
keyword.print $foo if $bar;
if ($bar) {print $foo;}
$bar && print $foo;
print $bar ? $foo :'';
use strict;
and use warnings;
“one of the characteristics of a
postmodern computer language is that it puts the
focus not so much onto the problem to be solved, but
rather onto the person trying to solve the problem.
[…] I claim that it was the first computer
language to do that. I'd also like to claim that, in
many ways, it's still the only language to do that.
How does Perl put the focus onto the
creativity of the programmer? Very simple. Perl is
humble. It doesn't try to tell the programmer how to
program.” — Larry Wall
print "postcode: $postcode";
print "postcode: \"$postcode\"";
print qq(postcode: "$postcode");
print qq(postcode (for Campsie): "$postcode");
qq
operator can use any kind of 'brackets' you like:qq[]; qq##; qq//; qq\\; qq{}; qq^^; qq!!; qq--;
push(@array,$variable);
push @array, $variable;
# @array does not exist yet.
$array[3] = 'Durian';
[ undef, undef, undef, 'Durian' ];
$a = 1;
$a++;
Now of course $a
contains 2
;$a = 'A';
$a++;
$a
contains B
$a = 'Z';
$a++; # what does $a contain now?
$a
contains AA
$str = 'the cat sat on the mat';
$str =~ s/cat/dog/;
$str
variable contains 'the dog sat on the mat'$str = 'the cat sat on the mat';
$str =~ s/cat/uc($&)/e;
$str
variable contains 'the CAT sat on the mat'$str = 'the cat sat on the mat';
$animal = substr($str,4,3);
$str = 'the cat sat on the mat';
substr($str,4,3) = 'goat';
$str
is 'the goat sat on the mat' even though the numbers don't add upopen(FILE,'<','/var/log/some.log');
while(<FILE>){
do_something();
}
# we don't open an external file
while(<DATA>){
do_something();
}
# so where's the file?
__DATA__
suburb,postcode
Campsie,2194
Canterbury,2193
Pyrmont,2009
/