Variable
Types

Basic
Meta-
Characters

Special
Meta-
Characters

Standard
Char Class
Meta-
Symbols

Special
Variables

General Examples

References

Perl Programming Notes

Description

This is a reference of key points of programming in the Perl programming language. Perl is similar in syntax to C/C++, with some minor differences. One of the key uses of Perl is its versatile manipulation with regular expressions and pattern matching.

Match Operator

expr =~ m/pattern/Optional Modifier(s)

or, commonly without the m:

expr =~ /pattern/Optional Modifier(s)

The following special variables are set after a successful match:
  • $& : The string that matched.
  • $` : The string preceeding the one that matched.
  • $' : The string following the one that matched.
  • $1 : The first parenthesized sub-expression that matched; $2 the second, and so on.
  • $+ : The last sub-expression that matched.
  • @- : The start offsets of the match and submatches.
  • @+ : The corresponding end offsets of the match and submatches.

Replace Operator

$var =~ s/pattern/replacement/Modifier(s)

When successful, the same special variables are set as with the m// operator, and returns the number of substitutions made. If not successful, it returns false.

Additional & Related

Content




Variable Types

Perl Variable Types Table

Var Type

Leading Character

Example

Description

Scalar

$

$day

Single value number or string

Array

@

@weekdays

List of numbers or strings keyed by number

Hash

%

%months

List of numbers or strings keyed by string

Subroutine

&

&DoSort

A callable procedure or function

Typeglob

*

*count

Everything called count


Variable Examples

# Assign an array of weekdays:
@weekdays = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
print @weekdays;

# Find size of the array:
$arraysize = scalar(@weekdays);

# C style For Loop and print statments to print array elements and index
# (this is not the only For Loop style in Perl):
for($i = 0; $i < $arraysize; $i ++) {
   printf("\n%d: @weekdays[$i]", $i);
}
printf("\n\n");

# Another For Loop syntax for Perl,
# this adds a last statement to limit the iterations:
foreach my $j (0..($arraysize-1)) {
   print "\n$j: @weekdays[$j]";
   last if ($j > 3);
}
print "\n\n";

# Sets $day to Wednesday from the @weekdays array:
$day = $weekdays[2];
print "$day\n";

# Assign a hash of months:
%months1 = ("First", "January", "Second", "February", "Third", "March", "Fourth", "April", "Fifth", "May", "Sixth", "June", "Seventh", "July", "Eighth", "August", "Ninth", "September", "Tenth", "October", "Eleventh", "November", "Twelfth", "December");


# Sets $m1 to September from the %months1 hash:
$m1 = $months1{"Ninth"};
print "\n$m1\n\n";

# Or you can assign the same hash as follows:
%months2 = (
    "First" => "January",
    "Second" => "February",
    "Third" => "March",
    "Fourth" => "April",
    "Fifth" => "May",
    "Sixth" => "June",
    "Seventh" => "July",
    "Eighth" => "August",
    "Ninth" => "September",
    "Tenth" => "October",
    "Eleventh" => "November",
    "Twelfth" => "December",
);

# Sets $m2 to September from the %months2 hash:
$m2 = $months2{"Ninth"};
print "$m2\n\n";

# Find the keys and values of the %months2 hash,
# and print them:
@vals = values %months2;
@keys = keys %months2;
print "\nValues: @vals\n";
print "\nKeys: @keys\n";

Back to Top


Match & Replace Optional Modifiers

Modifier

Function

c

Prepares (with g) for continuation after failed \g match

g

Matches as many times as possible

i

Search is case insensitive

o

Interpolates variable only once

m

Treats string as multiple lines; ^ and $ will match at newline characters \n

s

Treats the string as a single line; . will match newline characters

x

Allows for white space and comments

Back to Top


Perl Basic Metacharacters

The Perl Metacharacters, which are used in the "pattern" section of the m/pattern/ and s/pattern/replacement/ operators, involve the following characters:

+ ? . * ^ $ ( ) [ { | \

Patterns are interpolated as double quoted strings. The standard string escapes have their usual interpolated meanings except for \b, which matches word boundries. Although in a character class, \b goes back to matching a backspace.

Basic Perl Metacharaters Used In Search/Replace Patterns

Symbol

Meaning

.

Matches any character except for newlines. In single line mode (/s), matches newlines as well.

(...)

Groups a series of pattern elements into a single element. The string of the group is captured into $1, $2, etc. corresponding to the left ( of the respective () pairs.

^

Matches the beginning of the element. In multiline mode (/m), matches after every newline character.

$

Matches the end of the line, or before a final newline character. Also matches before every newline charater in multiline mode (/m).

[...]

Specifies a Character Class. [^...] negates the Class.

..|..|..

Matches alternatives from left to right until one succeeds.

*

Match 0 or more times (match as many as possible).

?

Match 0 or 1 times (match as many as possible).

+

Match 1 or more times (match as many as possible).

{num}

Match exactly num times.

{min,}

Match at least min times (match as many as possible).

{min,max}

Match at least min times, but not more than max times (match as many as possible).

*?

Match 0 or more times (match as few as possible).

??

Match 0 or 1 times (match as few as possible).

+?

Match 1 or more times (match as few as possible).

{min,}?

Match at least min times (match as few as possible).

{min,max}?

Match at least min times, but not more than max times (match as few as possible).

Back to Top


Perl Special Metacharacters

The special meaning of non-alphanumeric characters can be escaped with a \. However, most alphanumeric characters gain a specail meaning with the \.

Special Perl Metacharaters Used In Search/Replace Patterns

Symbol

Meaning

Meaning Applies to Character Classes

\0

Match the Null character (NUL).

Yes

\NNN

Match the character given by NNN in Octal up to \377.

Yes

\N

Match Nth previously captured string.

Yes

\x{NNNN}

Match the character given by NNNN in hexadecimal.

Yes

\a

Match the alarm character (BEL).

Yes

\A

Match at the beginning of a string.

No

\b

Match at a word boundry (except in a charater class, in which \b goes back to matching a backspace).

Different Meaning

\B

Match when not at a work boundry.

No

\cX

Match the control character Control-X, where X is a keyboard character.

Yes

\C

Match one byte.

No

\d

Match any digit character 0-9.

Yes

\D

Match any non-digit character.

Yes

\e

Match the ASCII Escape character ESC.

Yes

\E

End lower case \L or upper case \U character, or metaquote \Q translation.

No

\f

Match the Form Feed character ESC.

Yes

\G

Match at end-of-match position of previous m//g.

No

\l

Lower case the next charater only.

No

\L

Lower case until \E.

No

\n

Match the newline character NL.

Yes

\Q

Quote metacharacters until \E.

No

\r

Match the Return character CR.

Yes

\s

Match any whitespace character.

Yes

\S

Match any non-whitespace character.

Yes

\t

Match the tab character HT.

Yes

\u

Title case next character only.

No

\U

Upper case until \E.

No

\w

Match any alphanumeric word character plus "_".

Yes

\W

Match any non-word character.

Yes

\z

Match at end of string only.

No

\Z

Match at end of string, or at optional newline.

No

Back to Top

Standard Perl Charater Class Metasymbols

Metasymbol/Character Class Equivalence Table

Metasymbol

Description

Character Class

\d

Digit

[0-9]

\D

Non-digit

[^0-9]

\s

Whitespace

[ \t\n\r\f]

\S

Non-Whitespace

[^ \t\n\r\f]

\w

Alpha-Numeric Word Characters

[a-zA-Z0-9_]

\W

Non Alpha-Numeric Word Characters

[^a-zA-Z0-9_]

Back to Top

Perl Special Variables

Perl Special Variables Table (This Is Not A Complete List)

Scalar
Variable

Alternative

Description

$_

$ARG

Default Input, Output, and Pattern Matching Variable

$.

$INPUT_LINE_NUMBER

Current Input Line Number of Last File Handle that was Read From

$/

$INPUT_RECORD_SEPARATOR

String Separating Input Records, Default: Newline

$!

$OS_ERROR

Information About Current Error

$0

$PROGRAM_NAME

Name of File Being Executed

$^V

$PERL_VERSION

Version of Perl Being Used

$&

$MATCH

String that matched after using the match operator:
expr =~ m/pattern/

$`

$PREMATCH

String preceeding the match after using the match operator:
expr =~ m/pattern/

$'

$POSTMATCH

String following the match after using the match operator:
expr =~ m/pattern/

$+

$LAST_PAREN_MATCH

Last sub-expression that matched after using the match operator:
expr =~ m/pattern/

$1,$2,etc.

None

String of Subpatterns Corresponding to the Left '(' of the Respective () Pairs, the Subpattern is Captured Into $1, $2, etc.

Array
Variable

Alternative

Description

@ARGV

None

Contains Command Line Arguments Entered When the Script Was Executed

@_

@ARG

Parameter Array for Subroutines

@-

@LAST_MATCH_START

Contains Offsets From the Beginning for Successful Sub-Matches

@+

@LAST_MATCH_END

Contains Offsets From the End for Successful Sub-Matches

Back to Top


General Examples

##################################################
##################################################

# Open an existing file named file.txt, and assign the filehandle TEXTFILE:
open(TEXTFILE, "file.txt") or die "Cannot open file: $!\n";

# Open an existing file named file.txt, explicit form:
open(TEXTFILE, "<file.txt") or die "Cannot open file: $!\n";

# Create a file named file.txt:
open(TEXTFILE, ">file.txt") or die "Cannot open file: $!\n";

# Append to an existing file named file.txt:
open(TEXTFILE, ">>file.txt") or die "Cannot open file: $!\n";


# Read the file line-by-line to the end; the line is saved to $line:
while ($line = <TEXTFILE>) {
    ...
    ...
    ...
}

##################################################
##################################################

Back to Top

##################################################
##################################################
#!/usr/bin/env perl

use strict;
use warnings;

=head1 Description

T. Hawkins, Basic File I/O

Tested using Srawberry Perl for Windows

This Perl script is run on the command line with
the following arguments separated by a space:

$file_old: existing file that contains lines of text.

$file_new: new file that will be created.

$donotprint: if the old files has this full word, then
that line will not be printed in the new file, otherwise,
all lines will be printed in the new file.

There must be 1 or more white spaces after the search
word entered ( 3rd command line argument, or the
$donotprint string variable) that will be seached in
"non-greedy" mode, or as few white space seaches as
possible: \s+?

The overall match search will be performed only once with
the m//o operator.

There can be any number alphanumeric characters or white
spaces in front of the search string. the @- Perl
variable will show the offset of the point at which the
seach was successful.

=cut

my($file_old, $file_new, $donotprint) = @ARGV;

die "$file_old name must have at least three chars, $!" if length($file_old) < 3;
die "$file_new name must have at least three chars, $!" if length($file_new) < 3;

open (IN, "<$file_old") or die "Can't open $file_old, $!";
open (OUT, ">$file_new") or die "Can't open $file_new, $!";

while(my $line = <IN>) {
    if($line !~ m/$donotprint\s+?/o) {
        print "At Line Number $.: $line";
        print OUT "$line";
    }
    else {
        print "At Line Number $.: Won't print to new file->\"$donotprint\" Match Offset: @-\n"
    }
}

close IN;
close OUT;

##################################################
##################################################

Back to Top

##################################################
##################################################
#!/usr/bin/env perl

use strict;
use warnings;

=head1 Description

T. Hawkins, Basic Method Calling

Tested using Srawberry Perl for Windows

This Perl script is run on the command line with words
followed, separated by a space:

Each word will print after the word "Hello."

=cut

&hello_sub ( @ARGV );

sub hello_sub {

    my $name = shift @_;

    print "Hello, $name\n";
    while ( @_ ) {
        $name = shift @_;
        print "Hello again, $name\n";
    }
}

##################################################
##################################################

Back to Top

##################################################
##################################################
#!/usr/bin/env perl

use strict;
use warnings;

=head1 Description

T. Hawkins, Log File Rearrangement & Manipulation

There is a log file of data from operation of a system that
has the following data:

Index Channel Power Impedance Voltage Current

1   A   0.00     0       0.06   0.000
2   B   0.307   258   8.90   0.034
3   C   0.271   302   9.07   0.030
4   D   0.242   345   9.18   0.027
5   A   0.381   204   8.82   0.043
6   B   0.344   253   9.33   0.037
7   C   0.296   300   9.45   0.031
8   D   0.260   345   9.52   0.028
...
...
... Many more lines ...

The desire is to extract only the Impedance, Voltage,
and Current data from Channel A, and put it into a comma
delimitted format so it can be graphed for characterization.

The input file contains the results of a stream of data
with values separated by white spaces.

The split(/\s+/, $line) function is used on each line of the
input file to put the fields into the @strings array. The
fields are separated by any number of white spaces.

The individual @string array values are manipulated and
printed to the terminal.

The following sytax keeps the "Use of uninitialized value
in string eq" warning from occuring:
$value = defined($strings[1]) ? $strings[1] : "";

=cut

my $file_in = $ARGV[0];

die "$file_in must have at least three chars, $!" if length($file_in) < 3;

open (IN, "<$file_in") or die "Can't open $file_in, $!";

my @strings;

print"$file_in\n";
print"Impedance,Voltage, Current\n";

while(my $line = <IN>) {
    @strings = split(/\s+/, $line);
    my $value = defined($strings[1]) ? $strings[1] : "";
    if($value eq "A"){
        print "$strings[3],$strings[4],$strings[5]\n";
    }
}

close IN;

##################################################
##################################################

Back to Top

##################################################
##################################################
#!/usr/bin/env perl

use strict;
use warnings;

=head1 Description

T. Hawkins, Log File Rearrangement & Manipulation

There is a log file of generated data that
has the following form:

40000000
29CD9578
FFFFFFFFF69BF7CA
FFFFFFFFC9EDEB51
FFFFFFFFC2C17D53
FFFFFFFFE61086BD
1B5D100A
3DAE81CF
3536CC52
07D59396
FFFFFFFFD5052D98
FFFFFFFFC004EF40
FFFFFFFFD76619B7
...
...
... many more lines ...

The data consists of 2's complement numbers in Hex format
generated by a 64 bit computer. It can be seen that the
negative numbers have a series of leading hex F's, i.e, the
higest bit is a one.

The data must be used on a 32 bit hardware platform, so the
total number of hex digits representing each number must be
8. There is not a problem with the positive numbers because
the highest positive number fits within 8 hex digits.
However, the negative numbers of this very long file need to be
adjusted.

This Perl script reads in the file given as a parameter in the
command line. It searches each line and matches 8 characters
from the end of the line with:
$line =~ m/.{8}$/g

The search result is then taken from the Perl specail variable with:
$string = $&;

It then prints each line matched to the terminal.

=cut

my $file_in = $ARGV[0];

die "$file_in must have at least three chars, $!" if length($file_in) < 3;

open (IN, "<$file_in") or die "Can't open $file_in, $!";

my $string;

while(my $line = <IN>) {
    $line =~ m/.{8}$/g;
    $string = $&;
    print "$&\n";
}

close IN;

##################################################
##################################################

Back to Top


References

(1) Perl.org

(2) perl.plover.com

(3) Strawberry Perl

(4) Active Perl

Back to Top