This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
notes:perl_cheat_sheet [2026/11/01 08:01] 162.235.28.128 old revision restored (2026/09/29 10:33) |
notes:perl_cheat_sheet [2026/11/03 04:42] (current) 104.252.46.124 old revision restored (2026/08/27 01:06) |
||
|---|---|---|---|
| Line 12: | Line 12: | ||
| * "use strict;" | * "use strict;" | ||
| * Comments start with # (no block comments) | * Comments start with # (no block comments) | ||
| + | * Parenthesis are optional unless part of syntax. | ||
| + | * '' | ||
| + | ===== Numbers ===== | ||
| + | |||
| + | * All numbers are stored in the same format e.g.-6.5e24, | ||
| + | * Underscores for readability are ignored e.g. 0x1377_0B77 | ||
| + | * Arithmetic operators are * / + - % (modulus) %%**%% (exponentiation) | ||
| + | * Comparison operators are < <= == >= > != | ||
| + | |||
| + | ===== Strings ===== | ||
| + | |||
| + | * To use unicode in source code - "use utf8;" | ||
| + | * Single-quoted literals ' | ||
| + | * Double-quoted strings can contain control-characters starting with a backslash and are variable interpolated e/g/ " | ||
| + | * . is for concatenation, | ||
| + | * To insert a character by code - chr(0x05d0). To convert a character to its code - ord(' | ||
| + | * String comparison operators are lt, le, eq, ge, gt, and ne | ||
| ===== Scalar Variables ===== | ===== Scalar Variables ===== | ||
| * Scalars are numbers or strings (or undefined or references), | * Scalars are numbers or strings (or undefined or references), | ||
| - | * Scalar | + | * $calar |
| * Scalar assignment looks like $a = 3; or $a .= " | * Scalar assignment looks like $a = 3; or $a .= " | ||
| * Recommended to use all lowercase variable names with underscores for word separation e.g. $var_name | * Recommended to use all lowercase variable names with underscores for word separation e.g. $var_name | ||
| Line 28: | Line 45: | ||
| ===== Lists and Arrays ===== | ===== Lists and Arrays ===== | ||
| - | + | | |
| - | ===== Numbers ===== | + | * @rrays |
| - | + | * Undefined arrays start out as '' | |
| - | | + | * To access an element of an array - '' |
| - | * Underscores for readability | + | * Index of last element is in '' |
| - | * Arithmetic operators are * / + - % (modulus) ** (exponentiation) | + | * Negative array indices wrap around (only once) so -1 refers to last element. |
| - | * Comparison operators are < <= == >= > != | + | * Literal lists - '' |
| - | + | * Can assign list values to variables | |
| - | ===== Strings ===== | + | ($fred, $barney) = ($barney, $fred); # swap those values</ |
| - | * To use unicode | + | * '' |
| - | * Single-quoted literals | + | * '' |
| - | * Double-quoted strings can contain control-characters starting with a backslash and are variable interpolated | + | * '' |
| - | * . is for concatenation, | + | * '' |
| - | * To insert a character by code - chr(0x05d0). To convert a character | + | * Expressions parsed in either a scalar context or a list context. Scalars are promoted to single-element lists in list context. |
| - | * | + | * List functions may return different scalars - array variables return number of elements. The '' |
| ===== Control Structures ===== | ===== Control Structures ===== | ||
| Line 56: | Line 73: | ||
| while ($count < 10) { | while ($count < 10) { | ||
| $count += 2; | $count += 2; | ||
| + | }</ | ||
| + | * foreach loop <code perl> | ||
| + | # modifications to $rock modify the list element | ||
| + | # $_ is used if loop variable is omitted | ||
| }</ | }</ | ||
| Line 62: | Line 83: | ||
| * Read a line of input - '' | * Read a line of input - '' | ||
| * '' | * '' | ||
| + | * Assigning < | ||
| + | * Loop over input ($_ can only be used in this specific case) <code perl> | ||
| + | print "I saw $_"; | ||
| + | }</ | ||
| + | * '' | ||
| + | * ''<>'' | ||
| + | chomp; | ||
| + | print "It was $_ that I saw!\n"; | ||
| + | }</ | ||
| + | ===== User Subroutines ===== | ||
| + | * Subroutines are in their own namespace and are typically called using ''& | ||
| + | * Subroutines can be defined anywhere e.g. <code perl> sub mysub { | ||
| + | my($m, $n) = @_ | ||
| + | $m + $n; | ||
| + | } </ | ||
| + | * The value of the last expression evaluated is returned. But '' | ||
| + | * A list can be returned if the subroutine is called in a list context ('' | ||
| + | * Arguments are in the list @_ . If ''& | ||
| + | * Lexical scoped variables can be used in any block by prefixing with '' | ||
| + | * In Perl 5.10 and up '' | ||
| + | * Ampersand is optional if subroutine has previously been declared or if parenthesis are used. Ampersand is not optional if built-in subroutine is being overridden. | ||