Difference between revisions of "Python-Perl cheatsheet"
Devicerandom (Talk | contribs) (→Static arrays) |
Devicerandom (Talk | contribs) |
||
Line 128: | Line 128: | ||
| $0 | | $0 | ||
| | | | ||
+ | |- | ||
+ | | Multiple file arguments stream | ||
+ | | ''see '''[http://docs.python.org/library/fileinput.html fileinput]''' module'' | ||
+ | | <> | ||
+ | | Comntains sequentially all the file stream from all files in program arguments, like ./script.pl file1 file2 file3 | ||
|- | |- | ||
|} | |} |
Revision as of 14:57, 2 May 2011
This is so far based on my study of Learning Perl (O'Reilly), also known as "the llama book".
See also the Perl Phrasebook on wiki.python.org
what | Python | Perl | notes |
---|---|---|---|
null type | None | undef | not exactly identical |
String concatenation | "py"+"thon" | "pe"."rl" | |
if x == y | if ( $x == $y ) and if ( $x eq $y) | Perl requires parenthesis in a comparison (same for while). Perl uses different comparators for numbers and strings | |
Arrays | |||
declare a list | [1,2,3] | (1,2,3) | note that arrays contain only scalars! (1,2, (3,4), 5) becomes (1,2,3,4,5) |
array length | len(array) | $#array + 1 | Python returns the length of array. Perl gives me the index of the last element. Also, the array value in a scalar context is (usually) its size. |
range(1,100) | (1..100) | ||
create a list from whitespaced values | "fred barney wilma dino".split() | qw( fred barney wilma dino); | You can use separators others than parenthesis, e.g. qw ! fred barney wilma dino ! |
multiple assignment | fred, barney, dino = "flintstone", "rubble", None | ($fred, $barney, $dino) = ("flintstone", "rubble", undef); | |
array concatenation | a+b | (@a, @b) | you can also mix like (@a, 3, @b, undef) |
array.append(item) | push @array, item; | See also Perl's unshift() for attaching to the front of array | |
val = array.pop() | val = pop @array; | See also Perl's shift() for removing from the front of array | |
List iteration | for item in array | foreach $item (@array) | Note that $item is the real element of @array, so if you modify $item, you modify the array content |
list.reverse() | @list = reverse @list; | Python is in place, Perl is not. Same for sort. | |
Functions and subroutines | |||
Subroutine/function declaration | def function(): ... | sub function { ... } | |
return x | return x or [last evaluated expression is return value] | Perl has an explicit return statement, but it's facultative. A return-less sub won't return "undef", but the last expression evaluated! | |
Arguments of a function | def function(arg1,arg2...) | sub function { ... $_[0] , $[1] ... } | Perl doesn't have explicit arguments; args are stored in the @_ default array. |
Name of argument variables | def function(arg1,arg2...) | sub function { my($arg1, $arg2) = @_; } | You can name arguments by declaring them using the @_ default array |
Static variables in function scope | (define a generator ; see yield ) ; (use namespace within function, like def foo(): foo.myvar=1) | state $myvar=1; | Defines a variable which is statically defined but within function namespace -so it's not resetted when function is called, it maintains its state. |
Input / output | |||
x = raw_input() | $x = <STDIN>; | Interestingly if you use a list context, like in @x = <STDIN> , you get multiline stdin input. | |
sys.argv | @ARGV | ||
Program's own name | sys.argv[0] | $0 | |
Multiple file arguments stream | see fileinput module | <> | Comntains sequentially all the file stream from all files in program arguments, like ./script.pl file1 file2 file3 |
Traps
Static arrays
- To have a valid
state @array
- you need to
use 5.010;
while/foreach memory consumption
- This:
while (<STDIN>) { ...
- reads one line at a time.
- This:
foreach (<STDIN>) { ...
- instead loads all STDIN into memory before processing it!