Differences
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 [2013/08/31 23:37] smthng [Input/Output] |
notes:perl_cheat_sheet [2013/09/22 19:28] (current) smthng [Hashes] |
||
|---|---|---|---|
| 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 ===== | ||
| Line 44: | Line 61: | ||
| * Expressions parsed in either a scalar context or a list context. Scalars are promoted to single-element lists in list context. | * 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 '' | * List functions may return different scalars - array variables return number of elements. The '' | ||
| - | ===== Numbers ===== | ||
| - | * All numbers are stored in the same format e.g.-6.5e24, | + | ===== Hashes ===== |
| - | * Underscores for readability are ignored e.g. 0x1377_0B77 | + | |
| - | * Arithmetic operators are * / + - % (modulus) ** (exponentiation) | + | |
| - | * Comparison operators are < <= == >= > != | + | |
| - | ===== Strings ===== | + | * A hash is a list indexed by a string (key) - a collection of key-value pairs. '' |
| - | * To use unicode in source code - "use utf8;" | + | * Uses scalable, efficient algorithms. Used to be called associative arrays. |
| - | * Single-quoted literals | + | * '' |
| - | * Double-quoted strings can contain control-characters starting with a backslash and are variable interpolated | + | * Assigning a hash to an array unwinds (flattens) it. |
| - | * . is for concatenation, x for string repetition. | + | * To initialize a hash :<code perl> |
| - | * To insert | + | %some_hash = ( ' |
| - | | + | * When using a big arrow (a fat-comma) or when accessing |
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * To iterate over hash : <code perl> | ||
| + | | ||
| + | print "$key => $hash{$key}\n"; | ||
| + | * %ENV hash holds environment variables. | ||
| ===== Control Structures ===== | ===== Control Structures ===== | ||
| Line 72: | Line 92: | ||
| $count += 2; | $count += 2; | ||
| }</ | }</ | ||
| - | * foreach loop <code perl> | + | * foreach loop <code perl> |
| # modifications to $rock modify the list element | # modifications to $rock modify the list element | ||
| # $_ is used if loop variable is omitted | # $_ is used if loop variable is omitted | ||
| Line 81: | Line 101: | ||
| * Read a line of input - '' | * Read a line of input - '' | ||
| * '' | * '' | ||
| - | * Assigning < | + | * Assigning < |
| + | * Loop over input ($_ can only be used in this specific case) <code perl> | ||
| + | print "I saw $_"; | ||
| + | }</ | ||
| + | * '' | ||
| + | * ''<>'' | ||
| + | chomp; | ||
| + | print LOGFILE "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 my $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. | ||