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/04 09:52] 142.173.45.18 old revision restored (2026/10/23 19:07) |
notes:perl_cheat_sheet [2026/11/05 19:08] (current) 190.140.115.95 old revision restored (2026/08/06 16:20) |
||
|---|---|---|---|
| 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. | ||
| + | * '' | ||
| + | * * die can be used to exit early $! will contain any system error message. Without \n at the end, perl will append line number to error message. | ||
| ===== Numbers ===== | ===== Numbers ===== | ||
| Line 73: | Line 75: | ||
| $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 82: | Line 84: | ||
| * 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 "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 ===== | ===== User Subroutines ===== | ||
| Line 91: | Line 112: | ||
| $m + $n; | $m + $n; | ||
| } </ | } </ | ||
| - | * The value of the last expression evaluated is returned. | + | * 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 ''& | * Arguments are in the list @_ . If ''& | ||
| * Lexical scoped variables can be used in any block by prefixing with '' | * 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. | ||