Catalog / Perl Programming Cheatsheet
Perl Programming Cheatsheet
A concise reference for Perl syntax, built-in functions, regular expressions, and common programming idioms. This cheat sheet provides a quick guide to the essential elements of Perl for both beginners and experienced programmers.
Basic Syntax and Data Types
Variables
Scalars |
Start with
|
Arrays |
Start with
|
Hashes |
Start with
|
Variable Interpolation |
Variables are interpolated in double-quoted strings.
|
Context |
Perl uses scalar, list, and void context to determine how expressions are evaluated. |
Operators
Arithmetic |
|
String |
|
Comparison |
Numeric: |
Logical |
|
Assignment |
|
Control Structures
Conditional Statements:
|
Loops:
|
Loop Control: |
Subroutines and Modules
Subroutines
Defining a Subroutine |
|
Calling a Subroutine |
|
@_ |
Array containing arguments passed to the subroutine. |
Return Values |
Subroutines can return values using the
|
Local Variables |
Use |
Modules
Using Modules |
|
Creating Modules |
Create a
|
|
Used to export functions from the module’s namespace. |
|
Array of function names to export. |
Always return |
Modules must return true to indicate successful loading. |
Common Built-in Functions
|
|
|
|
|
Regular Expressions
Basic Matching
Pattern Matching |
|
Substitution |
|
Translation |
|
Quantifiers
|
Zero or more times |
|
One or more times |
|
Zero or one time |
|
Exactly n times |
|
n or more times |
|
Between n and m times |
Character Classes
|
Any character except newline |
|
Digit (0-9) |
|
Word character (a-z, A-Z, 0-9, _) |
|
Whitespace character (space, tab, newline) |
|
Any of the characters a, b, or c |
|
Any character except a, b, or c |
Anchors
|
Start of the string |
|
End of the string |
|
Word boundary |
Modifiers
|
Case-insensitive |
|
Global (match all occurrences) |
|
Multiline (treat string as multiple lines) |
|
Treat string as single line |
|
Ignore whitespace in pattern |
File I/O and System Interaction
File Handling
Opening Files |
|
Reading from Files |
|
Writing to Files |
|
Closing Files |
|
$! |
Contains the system error message. |
Command Line Arguments
@ARGV |
Array containing command-line arguments.
|
Getopt::Long |
Module for parsing command-line options.
|
System Calls
|
Executes a system command.
|
|
Captures the output of a system command.
|
|
Replaces the current process with a new one. Terminates the current script after successful execution.
|
Environment Variables
%ENV |
Hash containing environment variables.
|