Looks like the main use of perl these days is to maintain old code which used perl. Perhaps, Python has taken over the role that perl used to play earlier. Still, perl run faster than python (especially for regex operations).
Resource:
#
shift
: remove the first element in array and return that valueunshift
: prefix the given elements to the beginning of an arraypush
: append elements to end of arraypop
: remove last element of array and return that value@arr = ('Hi.', 'Jack', 'and', 'Jill');
print "@arr\n";
push(@arr, 'went', 'up');
print "@arr\n";
unshift(@arr, 'Hello', 'and');
print "@arr\n";
my $v1 = pop(@arr);
print "v1: $v1\n";
print "@arr\n";
my $v2 = shift(@arr);
print "v2: $v2\n";
print "@arr\n";
# Hi. Jack and Jill
# Hi. Jack and Jill went up
# Hello and Hi. Jack and Jill went up
# v1: up
# Hello and Hi. Jack and Jill went
# v2: Hello
# and Hi. Jack and Jill went
qw()
: quoted wordchomp()
: remove newline from end of string ??lc()
: convert string to lowercaselength()
: Lents of a string$str1 . $str2
sprintf
/
, but can be other symbols too.my $s = q/"String" not yet over/;
> '"String" not yet over'
print $s;
> "String" not yet over
https://perldoc.perl.org/variables
$
$
looks like 's'@
@
looks like 'a'use strict;
to make perl warn you of implicit global variables.my
to declare local variables.
my
vars: my ($a, $b);
https://perldoc.perl.org/perlsub
sub
sub foo { }
@_
: array containing function args
$_[0]
: 1st arg, $_[1]
: 2nd arg, etcIf there are two variables in current scope which has the same name (say, $x
), use ::$x
to unshadow outer variable.
// Global var set$pi = 3.14;
print "Pi is $pi\n";
{print "Inside block\n";
reset
// Global var $pi = 3.1415;
print "Inner Pi is $pi\n";
print "Outer pi is $::pi\n";
// Local var setmy $pi = 3.141592;
print "Inner Pi is $pi\n";
print "Outer pi is $::pi\n";
print "Leaving block\n";
}
local var is no longer in scope.
// The
// Latest value of global var is printed.print "Pi is $pi\n";
# Pi is 3.14
# Inside block
# Inner Pi is 3.1415
# Outer pi is 3.1415
# Inner Pi is 3.141592
# Outer pi is 3.1415
# Leaving block
# Pi is 3.1415
https://www.perltutorial.org/perl-array/
Number of elements: @arr
Highest index: $#arr
Stack operations:
push()
pop()
Queue operations:
shift()
: remove first element of arrayunshift()
: add elements to front of arraypop()
: remove last element of arraySort array: sort @arr
$arr[i]
$arr[-2..-1]
Hash variable names start with a %
.
%hash={"one"=>1,"two"=>2,"three"=>3}
print $hash{"one"}
Use -e
option.
perl -e 'print("Hey!\n")'
Hey!
# $elem is scalar, @elems is an array
foreach $elem in @elems {
# ...
}
WARNING: Probably never tried any of the following…
Install a module: cpan Module::Name
cpan -i Module::Name
(∵ the -i
switch is default)Installation of new packages: cpan <package-name>
cpan Unicode::GCString
cpan YAML::Tiny
cpan File::HomeDir
Run o conf init
inside cpan to re-run configuration
cpanm
(cpanminus): something to supplement cpan ??
cpan
.cpan App::cpanminus
or using distro package manager.perltidy
: automatic code formatter
Perl::Tidy
rlwrap perl -de1
my
variables ??reply
: https://metacpan.org/pod/Reply
cpanm Reply