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/09/15 02:33] 85.90.197.93 old revision restored (2013/08/22 04:52) |
notes:perl_cheat_sheet [2026/09/17 10:21] (current) 188.3.199.7 old revision restored (2026/09/11 18:08) |
||
|---|---|---|---|
| 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 47: | ||
| * 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 '' | * Undefined arrays start out as '' | ||
| * To access an element of an array - '' | * To access an element of an array - '' | ||
| Line 38: | Line 55: | ||
| * Can assign list values to variables e.g. <code perl> | * Can assign list values to variables e.g. <code perl> | ||
| ($fred, $barney) = ($barney, $fred); # swap those values</ | ($fred, $barney) = ($barney, $fred); # swap those values</ | ||
| - | ===== Numbers ===== | + | |
| - | + | * '' | |
| - | | + | * '' |
| - | | + | * '' |
| - | * Arithmetic operators are * / + - % (modulus) ** (exponentiation) | + | * Expressions parsed in either |
| - | * Comparison operators are < <= == >= > != | + | * List functions may return different scalars |
| - | + | ||
| - | ===== 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 | + | |
| ===== Control Structures ===== | ===== Control Structures ===== | ||
| Line 65: | Line 74: | ||
| 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 71: | Line 84: | ||
| * 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"; | ||
| + | }</ | ||
| + | * '' | ||
| + | print sort <>; | ||
| + | * C-like printf function %g for number auto-format, | ||
| + | my $format = "The items are: | ||
| + | printf $format, @items; | ||
| + | printf "The items are: | ||
| + | </ | ||
| + | * Filehandles can be barewords (upper-cased) or variables. Special filehandles are : STDIN, STDOUT, STDERR, DATA, ARGV, and ARGVOUT .<code perl> | ||
| + | open BEDROCK, '> | ||
| + | open LOG, '>>: | ||
| + | open BEDROCK, '>: | ||
| + | binmode STDOUT, ': | ||
| + | ===== 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. | ||