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 label
×
last label
×
Input and output
Perl Python Notes
say
print
open
.open
STDIN
sys.stdin
STDOUT
sys.stdout
`command`
os.popen ('command').read ()
Libraries
Perl Python Notes
use Module 'function';
from module import function
new
__init__
Calling object constructers "new" is only a convention in Perl.
package stuff
class stuff
push @ISA, parent;
use parent parent;
class stuff (parent)
print FH stuff
fh.write (stuff)
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 (@A)
len (A)
grep
filter
x
*
Repetition operator.
return
return
Subroutines and functions
Perl Python Notes
sub name
def name
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 (x, base=16)
?
yield
×
round
?
slice
ref
type
chr
unichr
cmp
cmp
<=>
×
×
divmod
use utf8;
×
$x = undef
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


Copyright © Ben Bullock 2009-2019. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer