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).
- Possible expansion of PERL: Practical Extraction and Reporting Language
- Perl 5 is current perl
- Perl 6 turned out to be Raku
Resource:
- Perl Hacks: Tips & Tools for Programming, Debugging, and Surviving - Shane Warden, Damian Conway, Curtis 'Ovid' Poe
General
- Comments: Starts with
#
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
Strings
qw()
: quoted wordchomp()
: remove newline from end of string ??lc()
: convert string to lowercaselength()
: Lents of a string- String concatenation:
$str1 . $str2
- String formatting:
sprintf
- Also see string interpolation: https://perldoc.perl.org/perlop#Gory-details-of-parsing-quoted-constructs
Quoted strings
- q: single quoted string
- qq: double quoted string
- Delimiter is usually
/
, but can be other symbols too.
- Delimiter is usually
my $s = q/"String" not yet over/;
> '"String" not yet over'
print $s;
> "String" not yet over
Variables
https://perldoc.perl.org/variables
- Scalar variables start with:
$
- The
$
looks like 's'
- The
- Array variables start with:
@
- The
@
looks like 'a'
- The
- By default, variables are global.
- Include
use strict;
to make perl warn you of implicit global variables.
- Include
- Use
my
to declare local variables.- Declaration of multiple
my
vars:my ($a, $b);
- Declaration of multiple
Functions
https://perldoc.perl.org/perlsub
- Subroutines: Functions in perl
- Defined with
sub
- Eg:
sub foo { }
- Defined with
@_
: array containing function args$_[0]
: 1st arg,$_[1]
: 2nd arg, etc
Multiple variable with same name
If 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
Arrays
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 array
Sort array:
sort @arr
Indexing
- Indexing starts from 0:
$arr[i]
- Negative indexing and index slices possible.
- Eg:
$arr[-2..-1]
- Eg:
Hashes
Hash variable names start with a %
.
%hash={"one"=>1,"two"=>2,"three"=>3}
print $hash{"one"}
Inline perl
Use -e
option.
perl -e 'print("Hey!\n")'
Hey!
Loop
# $elem is scalar, @elems is an array
foreach $elem in @elems {
# ...
}
cpan
WARNING: Probably never tried any of the following…
Install a module:
cpan Module::Name
- Same as:
cpan -i Module::Name
(∵ the-i
switch is default) - May take some time to finish.
- Same as:
Installation of new packages:
cpan <package-name>
- Examples:
cpan Unicode::GCString
cpan YAML::Tiny
cpan File::HomeDir
- Examples:
Run
o conf init
inside cpan to re-run configurationcpanm
(cpanminus): something to supplement cpan ??- Considered simpler to use than
cpan
. - Installable with
cpan App::cpanminus
or using distro package manager.
- Considered simpler to use than
Misc
perltidy
: automatic code formatter- Package:
Perl::Tidy
- Package:
- A perl repl: Many options, each with their own pros and cons.
rlwrap perl -de1
- Doesn't support
my
variables ??
- Doesn't support
reply
: https://metacpan.org/pod/Reply- Install with
cpanm Reply
- Install with
More
- Perl regex
- String formatting/templating
- Time management and formatting
- Example perl script (LaTeXTidy.pl): http://web.archive.org/web/20160822025224/http://bfc.sfsu.edu/LaTeXTidy-0.31.pl