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.

Ads

Real ECG

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

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

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

# Or you can assign the same hash as follows:
%months = (
    "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 $myMonth to September from the %months hash:
$myMonth = $months{"Ninth"};

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

$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

Tim Hawkins, 2013

Tested using Srawberry Perl for Windows

This Perl script is run on the command line with arguments
followed, 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.

=cut

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

die "$file_old must contain at least three characters, $!" if length($file_old) < 3;
die "$file_new must contain at least three characters, $!" 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

Tim Hawkins, 2013

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


References

(1) Perl.org

(2) perl.plover.com

(3) Strawberry Perl

(4) Active Perl

Back to Top