Perl


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:

General

Strings

Quoted strings

> my $s = q/"String" not yet over/;
'"String" not yet over'

> print $s;
"String" not yet over

Variables

https://perldoc.perl.org/variables

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";

  // Global var reset
  $pi = 3.1415;
  print "Inner Pi is $pi\n";
  print "Outer pi is $::pi\n";

  // Local var set
  my $pi = 3.141592;
  print "Inner Pi is $pi\n";
  print "Outer pi is $::pi\n";

  print "Leaving block\n";
}

// The local var is no longer in scope.
// 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/

Indexing

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…

Misc

More