Equivalents in Perl and Python
This is a list of equivalents in Perl and Python. The cross, ×, means that there is no equivalent function in the other language.
Strings | ||
---|---|---|
Perl | Python | Notes |
pack |
.pack |
|
chomp |
.rstrip ('\n') |
|
uc |
.upper () |
|
lc |
.lower () |
|
ucfirst |
.capitalize |
Python's "capitalize" also lowercases the string then capitalizes the
first letter, so it is like ucfirst (lc ($x)) in Perl. |
sprintf |
% |
|
qw |
× |
|
" |
× |
|
' |
", ' |
|
s |
.replace |
|
substr |
[:] |
|
split |
.split |
|
here document |
× |
|
. |
+ |
|
Control | ||
Perl | Python | Notes |
if |
if |
|
elsif |
elif |
|
else |
else |
|
unless |
× |
|
while |
while |
|
foreach, for |
for |
|
die |
raise |
|
next |
continue |
|
last |
break |
|
next |
× |
|
last |
× |
|
Input and output | ||
Perl | Python | Notes |
say |
||
open |
.open |
|
STDIN |
sys.stdin |
|
STDOUT |
sys.stdout |
|
` |
os.popen (' |
|
Libraries | ||
Perl | Python | Notes |
use |
from |
|
new |
__init__ |
Calling object constructers "new" is only a convention in Perl. |
package |
class |
|
push @ISA, |
class |
|
|
|
|
Lists | ||
Most of the Python things seem to go after the list variable name, whereas Perl's builtin functions usually go before the list. | ||
Perl | Python | Notes |
List::MoreUtils::zip |
zip |
|
List::Util::max |
max |
|
List::Util::min |
min |
|
pop |
.pop |
|
0..9 |
range (10) |
|
map |
map |
|
push |
+=, .append |
|
× |
.title |
|
join |
.join |
|
sort |
.sort |
|
scalar ( |
len ( |
|
grep |
filter |
|
x |
* |
Repetition operator. |
return |
return |
|
Subroutines and functions | ||
Perl | Python | Notes |
sub |
def |
|
sub |
lambda |
|
Other/Unclassified | ||
Perl | Python | Notes |
use strict |
× |
|
use warnings |
× |
|
my |
× |
|
our |
global |
|
; |
pass |
|
delete |
del |
|
eval { }; |
try |
|
× |
assert |
|
× |
getattr |
|
sprintf "%X" |
hex |
|
id |
||
? |
globals |
|
hex |
int ( |
|
? |
yield |
|
× |
round |
|
? |
slice |
|
ref |
type |
|
chr |
unichr |
|
cmp |
cmp |
|
<=> |
× |
|
× |
divmod |
|
use utf8; |
× |
|
|
x = None |
|
× |
True |
|
× |
False |
|
** |
** |
|
? |
rjust () |
|
keys |
.keys () |
|
values |
.values () |
|
× |
str |
|
goto |
× |
|
× |
__repr__ |
|
Operating system | ||
Perl | Python | Notes |
use Cwd; print getcwd (); |
import os print os.getcwd() |
|
Regular expressions, regexes | ||
Perl | Python | Notes |
qr |
re.compile |
|
/m |
re.findall |
|
/x |
re.VERBOSE, re.X |
Web links
-
Perl Phrasebook
A list of examples from a book called the "Perl Data Structures Cookbook" translated from Perl to Python.
Copyright © Ben Bullock 2009-2024. All
rights reserved.
For comments, questions, and corrections, please email
Ben Bullock
(benkasminbullock@gmail.com).
/
Privacy /
Disclaimer