Perl

perlintro – a brief introduction and overview of Perl
perlop – Perl operators and precedence
perlobj – Perl object reference
perldebug – Perl debugging
perlrequick – Perl regular expressions quick start

Perl Environment Include/Library Path

# perl -e "print qq(@INC)"
/usr/local/lib/perl5/site_perl/mach/5.20 /usr/local/lib/perl5/site_perl /usr/local/lib/perl5/5.20/mach /usr/local/lib/perl5/5.20 /usr/local/lib/perl5/site_perl/5.20 /usr/local/lib/perl5/site_perl/5.20/mach

# find /usr/local/lib/perl5 -name "*.pm"
/usr/local/lib/perl5/5.20/AnyDBM_File.pm
/usr/local/lib/perl5/5.20/App/Cpan.pm
...

Variable Type

Perl – Variables

Enum

Does Perl have an enumeration type?

RegEx

Regular Expressions and Matching
perl – strings comparison and regex

System Command-Line

perldoc: system
How can I store the result of a system command in a Perl variable?
How can I capture STDERR from an external command?

Name of the Perl script

How to get the name of Perl script that is running

print $0;

Command Line Arguments

  • Use $ARGV[n] to display argument.
  • Use $#ARGV to get total number of passed argument to a perl script.

Processing command line arguments – @ARGV in Perl
Perl Display And Pass Command Line Arguments With @argv
How to read Perl command-line arguments

my $argc= $#ARGV + 1;

my $name   = $ARGV[0];
my $number = $ARGV[1];
# or
my ($name, $number) = @ARGV;

Loops (for, foreach, etc.)

Perl for loop explained with examples

defined

perldoc: defined

Arrays

Perl Arrays

Note, when accessing a single element of an array the leading sigil changes from @ to $. This might cause confusion to some people, but if you think about it, it is quite obvious why.

@ marks plural and $ marks singular. When accessing a single element of an array it behaves just as a regular scalar variable.

# Declare an array
my @names;

# Declare and assign values:
my @names = ("Foo", "Bar", "Baz");

# Debugging of an array
use Data::Dumper qw(Dumper);
my @names = ("Foo", "Bar", "Baz");
say Dumper \@names;
# $VAR1 = [
#         'Foo',
#         'Bar',
#         'Baz'
#       ];

# foreach loop and perl arrays
my @names = ("Foo", "Bar", "Baz");
foreach my $n (@names) {
  say $n;
}
# Foo
# Bar
# Baz

# Accessing an element of an array
my @names = ("Foo", "Bar", "Baz");
say $names[0];

Indexing array

The indexes of an array start from 0. The largest index is always in the variable called $#name_of_the_array. So

my @names = ("Foo", "Bar", "Baz");
say $#names;

Will print 2 because the indexes are 0,1 and 2.

Length or size of an array

In Perl there is no special function to fetch the size of an array, but there are several ways to obtain that value. For one, the size of the array is one more than the largest index. In the above case $#names+1 is the size or length of the array.

In addition the scalar function can be used to to obtain the size of an array:

my @names = ("Foo", "Bar", "Baz");
say scalar @names;

Will print 3.

The scalar function is sort of a casting function that – among other things – converts an array to a scalar. Due to an arbitrary, but clever decision this conversion yields the size of the array.

Loop on the indexes of an array

There are cases when looping over the values of an array is not enough. We might need both the value and the index of that value. In that case we need to loop over the indexes, and obtain the values using the indexes:

my @names = ("Foo", "Bar", "Baz");
foreach my $i (0 .. $#names) {
  say "$i - $names[$i]";
}

prints:

0 - Foo
1 - Bar
2 - Baz

String

Perl string concatenation – How to concatenate strings with Perl

  • Perl string concatenation – Method #1 – Using ${name}
  • Perl string concatenation – Method #2 – using Perl’s dot operator
  • Perl string concatenation – Method #3 – using Perl’s join function
$name = 'foo';
$filename = "/tmp/${name}.tmp";

$name = checkbook'; 
$filename = '/tmp/' . $name . '.tmp'; 
# $filename now contains "/tmp/checkbook.tmp"

$name = 'checkbook'; 
$filename = join '', '/tmp/', $name, '.tmp'; 
# $filename now contains "/tmp/checkbook.tmp"

File I/O

How to tell perl to print to a file handle instead of printing the file handle? !!!

Writing to files with Perl
Perl – File I/O
How to remove one line from a file using Perl?
How do I delete a certain line from a file with Perl?
Insert a line at the beginning of a file

  • "<file.txt": read-only mode
  • ">file.txt": writing mode
  • "+<file.txt": updating without truncating
  • "+>file.txt": truncate the file first
  • ">>file.txt": append mode
  • "+>>file.txt": append mode with read
use strict;
use warnings;
 
my $filename = 'report.txt';
open(my $fh, '>', $filename) or die "Could not open file '$filename' $!";
print $fh "My first report generated by perl\n";
close $fh;
print "done\n";

Process List

Day 17: Checking process existence and listing processes (Proc::Find)
ps in perl?
Proc::Find – Find processes by name, PID, or some other attributes
Proc::ProcessTable – Perl extension to access the unix process table

# perl ...
Can't locate Proc/Find.pm in @INC (you may need to install the Proc::Find module)

Perl 6

Tutorials / Types in Perl 6 / Enums

Leave a Reply

Your email address will not be published. Required fields are marked *