Site Tools


notes:perl_cheat_sheet

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
notes:perl_cheat_sheet [2026/06/13 00:34]
114.119.152.46 old revision restored (2013/09/11 16:12)
notes:perl_cheat_sheet [2026/06/13 14:55] (current)
47.245.90.216 old revision restored (2026/06/04 13:11)
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.
 +  * ''@ARGV'' contains arguments, $0 contains program name.
  
 ===== Numbers ===== ===== Numbers =====
Line 18: Line 19:
   * All numbers are stored in the same format e.g.-6.5e24, 037 (octal), 0x2a13b (hex), 0b11010 (binary)   * All numbers are stored in the same format e.g.-6.5e24, 037 (octal), 0x2a13b (hex), 0b11010 (binary)
   * 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) %%**%% (exponentiation)
   * Comparison operators are < <= == >= > !=   * Comparison operators are < <= == >= > !=
  
Line 73: Line 74:
     $count += 2;     $count += 2;
 }</code> }</code>
-  * foreach loop <code perl>foreach $rock (@rocks) {+  * foreach loop <code perl>foreach my $rock (@rocks) {
   # 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 83:
   * Read a line of input - ''$line = <STDIN>;''   * Read a line of input - ''$line = <STDIN>;''
   * ''chomp'' removes a newline e.g. ''chomp($line=<STDIN>);''   * ''chomp'' removes a newline e.g. ''chomp($line=<STDIN>);''
-  * Assigning <STDIN> to a list reads all input up till EOF e.g. <code perl> chmop(@lines = <STDIN>);</code>+  * Assigning <STDIN> to a list reads all input up till EOF e.g. <code perl> chomp(@lines = <STDIN>);</code> 
 +  * Loop over input ($_ can only be used in this specific case) <code perl>while (<STDIN>) { 
 +    print "I saw $_"; 
 +}</code> 
 +  * ''foreach'' can also be used but uses more memory since it reads all input into memory first. 
 +  * ''<>'' iterates over all files in @ARGV (or STDIN if no args) like e.g. cat/sed/awk. <code perl>while (<>) { 
 +    chomp; 
 +    print "It was $_ that I saw!\n"; 
 +}</code> 
 +  * ''print'' takes list of items and sends all to STDOUT (unseparated). ''print @array;'' vs ''print "@array";'' <code perl>print <>;          # source code for 'cat' 
 +print sort <>;     # source code for 'sort'</code> 
 +  * C-like printf function %g for number auto-format,%10s, %-10d etc.<code perl>my @items = qw( wilma dino pebbles ); 
 +my $format = "The items are:\n" . ("%10s\n" x @items); 
 +printf $format, @items; 
 +printf "The items are:\n".("%10s\n" x @items), @items; 
 +</code> 
 +  * Filehandles can be barewords (upper-cased) or variables. Special filehandles are : STDIN, STDOUT, STDERR, DATA, ARGV, and ARGVOUT .<code perl>open CONFIG, '<dino';  # < is optional 
 +open BEDROCK, '>fred' || die "Cannot open fred: $!"; 
 +open LOG, '>>:encoding(UTF-8)','logfile'; # for perl >= 5.6 
 +open BEDROCK, '>:crlf', $file_name; # DOS-formatted output 
 +binmode STDOUT, ':encoding(UTF-8)';</code> 
 +===== User Subroutines ===== 
 + 
 +  * Subroutines are in their own namespace and are typically called using ''&mysub'' (sometimes the ampersand can be omitted). 
 +  * Subroutines can be defined anywhere e.g. <code perl> sub mysub { 
 +  my($m, $n) = @_ 
 +  $m + $n; 
 +} </code> 
 +  * The value of the last expression evaluated is returned. But ''return $a;'' can be used to return immediately. 
 +  * A list can be returned if the subroutine is called in a list context (''wantarray'' can be used to detect list or scalar context). 
 +  * Arguments are in the list @_ . If ''&mysub;'' is used, the parent's argument list is inherited. 
 +  * Lexical scoped variables can be used in any block by prefixing with ''my''
 +  * In Perl 5.10 and up ''state $x;'' can be used to declare a private variable that keep state between calls. 
 +  * 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.
notes/perl_cheat_sheet.1781336061.txt.gz · Last modified: 2026/06/13 00:34 by 114.119.152.46 · Currently locked by: 216.73.217.75