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:14] 100.6.3.96 old revision restored (2026/09/23 04:35) |
notes:perl_cheat_sheet [2026/11/03 04:42] (current) 104.252.46.124 old revision restored (2026/08/27 01:06) |
||
|---|---|---|---|
| Line 13: | Line 13: | ||
| * Comments start with # (no block comments) | * Comments start with # (no block comments) | ||
| * Parenthesis are optional unless part of syntax. | * 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 30: | Line 46: | ||
| * A list is an ordered set of elements (each of which is a scalar value), starting with position 0. | * A list is an ordered set of elements (each of which is a scalar value), starting with position 0. | ||
| - | * An array is a variable | + | * @rrays are variables |
| + | * Undefined arrays start out as '' | ||
| * To access an element of an array - '' | * To access an element of an array - '' | ||
| * Index of last element is in '' | * Index of last element is in '' | ||
| * Negative array indices wrap around (only once) so -1 refers to last element. | * Negative array indices wrap around (only once) so -1 refers to last element. | ||
| - | * | + | * Literal lists - '' |
| - | ===== Numbers ===== | + | * Can assign list values to variables |
| - | + | ($fred, $barney) = ($barney, $fred); # swap those values</ | |
| - | * All numbers are stored in the same format | + | * '' |
| - | * Underscores for readability are ignored e.g. 0x1377_0B77 | + | * '' |
| - | * Arithmetic operators are * / + - % (modulus) ** (exponentiation) | + | * '' |
| - | * Comparison operators are < <= == >= > != | + | * '' |
| - | + | * Expressions parsed in either a scalar context or a list context. Scalars | |
| - | ===== Strings ===== | + | * List functions may return different scalars |
| - | * 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 | + | |
| ===== Control Structures ===== | ===== Control Structures ===== | ||
| Line 62: | 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 68: | 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. | ||