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:
#
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);
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
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