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

@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

File handling

Classic way

No 3rd party libraries:

my $input_file  = shift || 'input.src';

open my $in_fh, '<', $input_file
    or die "Cannot open $input_file: ";

local ;
my $inp = <$in_fh>;

Or with Path::Tiny (not tried successfully):

use Path::Tiny;

my $in_fh = path($input_file);

die "Cannot read $input_file"
    unless $in_fh->exists && $in_fh->is_file;

my $inp = $in_fh->slurp;

print $inp;

Strings

Quoted strings

  • q: single quoted string
  • qq: double quoted string
    • Delimiter is usually /, but can be other symbols too.
> my $s = q/"String" not yet over/;
'"String" not yet over'

> print $s;
"String" not yet over

Variables

https://perldoc.perl.org/variables

Functions

https://perldoc.perl.org/perlsub

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

  • Indexing starts from 0: $arr[i]
  • Negative indexing and index slices possible.
    • Eg: $arr[-2..-1]

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 {
  # ...
}

REPL

perl itself doesn't seem to come bundled with a REPL, but its debugger serves the purpose.

One of these:

$ perl -d -e 0

Loading DB routines from perl5db.pl version 1.80
Editor support available.

Enter h or 'h h' for help, or 'man perldebug' for more help.

main::(-e:1):   0
  DB<1>

cpan

WARNING: Probably never tried any of the following…

Misc

Marpa::R2

  • There are multiple action styles
  • Multiple modes
    • SLIF: Scanless interface. Kind of automatic parsing
    • Classic: more manual parsing
  • Terms
    • G1 grammar : Parsing grammar
    • L0 grammar: Lexical grammar
  • ~: for lexing. ie, tokens
  • ::=: for parsing. ie, for parser

See:

More