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 20:59] 114.119.151.209 old revision restored (2026/10/15 17:45) |
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) | ||
| - | + | | |
| - | + | * '' | |
| - | ===== Scalar Variables ===== | + | |
| - | + | ||
| - | | + | |
| - | * Scalar variables start with $ and a letter or underscore, but can contain letters (including unicode), numbers or underscores. | + | |
| - | * Scalar assignment looks like $a = 3; or $a .= " | + | |
| - | * Recommended to use all lowercase variable names with underscores for word separation e.g. $var_name | + | |
| - | * print takes a single scalar or a list of scalars (separated by commas) | + | |
| - | * When a scalar is interpreted as a boolean, the following are //false// : 0, '0', %%'' | + | |
| - | * !! is a handy shortcut to convert any scalar to 0 or 1 (to represent a boolean) | + | |
| ===== Numbers ===== | ===== Numbers ===== | ||
| Line 28: | Line 19: | ||
| * All numbers are stored in the same format e.g.-6.5e24, | * All numbers are stored in the same format e.g.-6.5e24, | ||
| * Underscores for readability are ignored e.g. 0x1377_0B77 | * Underscores for readability are ignored e.g. 0x1377_0B77 | ||
| - | * Arithmetic operators are * / + - % (modulus) ** (exponentiation) | + | * Arithmetic operators are * / + - % (modulus) |
| * Comparison operators are < <= == >= > != | * Comparison operators are < <= == >= > != | ||
| ===== Strings ===== | ===== Strings ===== | ||
| + | |||
| * To use unicode in source code - "use utf8;" | * To use unicode in source code - "use utf8;" | ||
| * Single-quoted literals ' | * Single-quoted literals ' | ||
| Line 38: | Line 30: | ||
| * To insert a character by code - chr(0x05d0). To convert a character to its code - ord(' | * 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 | * String comparison operators are lt, le, eq, ge, gt, and ne | ||
| + | |||
| + | ===== Scalar Variables ===== | ||
| + | |||
| + | * Scalars are numbers or strings (or undefined or references), | ||
| + | * $calar variables start with $ and a letter or underscore, but can contain letters (including unicode), numbers or underscores. | ||
| + | * Scalar assignment looks like $a = 3; or $a .= " | ||
| + | * Recommended to use all lowercase variable names with underscores for word separation e.g. $var_name | ||
| + | * print takes a single scalar or a list of scalars (separated by commas) | ||
| + | * When a scalar is interpreted as a boolean, the following are //false// : 0, ' | ||
| + | * !! is a handy shortcut to convert any scalar to 0 or 1 (to represent a boolean) | ||
| + | * Variables have value '' | ||
| + | * '' | ||
| + | |||
| + | ===== Lists and Arrays ===== | ||
| + | |||
| + | * A list is an ordered set of elements (each of which is a scalar value), starting with position 0. | ||
| + | * @rrays are variables that store a list (separate namespace from scalar variables). @x refers to entire array x. | ||
| + | * Undefined arrays start out as '' | ||
| + | * To access an element of an array - '' | ||
| + | * Index of last element is in '' | ||
| + | * Negative array indices wrap around (only once) so -1 refers to last element. | ||
| + | * Literal lists - '' | ||
| + | * Can assign list values to variables e.g. <code perl> | ||
| + | ($fred, $barney) = ($barney, $fred); # swap those values</ | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * 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 51: | 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 57: | 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. | ||