Discuss New Concept,New Technic,New Tools, Including EAI,BPM,SOA,Tibco,IBM MQ,Tuxedo, Cloud,Hadoop,NoSQL,J2EE,Ruby,Scala,Python, Performance,Scalability,Distributed,HA, Social Network,Machine Learning.

September 27, 2011  Tagged with: , , ,

original:http://hyperpolyglot.org/scripting

a side-by-side reference sheet

php (1995) perl (1987) python (1991) ruby (1995)
versions used 5.3 5.12; 5.14 2.7; 3.2 1.8; 1.9
implicit prologue none use strict; import os, re, sys none
show version $php –version$ perl –version $python -V$ ruby –version
interpreter $php -f foo.php$ perl foo.pl $python foo.py$ ruby foo.rb
repl $php -a$ perl -de 0 $python$ irb
statement separator ;

statements must be semicolon terminated inside {}

; newline or ;

newlines not separators inside (), [], {}, triple quote literals, or after backslash: \

newline or ;

newlines not separators inside (), [], {}, “, ”, “”, or after binary operator or backslash: \

block delimiters {} {} offside rule {}
do end
assignment $v = 1;$v = 1; # does not return a value:
v = 1
v = 1
parallel assignment list($x,$y, $z) = array(1 ,2, 3); # 3 is discarded: list($x, $y) = array(1, 2, 3); #$z set to NULL:
list($x,$y, $z) = array(1, 2); ($x, $y,$z) = (1, 2, 3);
($x,$y) = (1, 2, 3);
# $z set to undef: ($x, $y,$z) = (1, 2);
x, y, z = 1, 2, 3
# raises ValueError:
x, y = 1, 2, 3
# raises ValueError:
x, y, z = 1, 2
x, y, z = 1, 2, 3
x, y = 1, 2, 3
# z set to nil:
x, y, z = 1, 2
swap list($x,$y) = array($y,$x); ($x,$y) = ($y,$x); x, y = y, x x, y = y, x
compound assignment operators: arithmetic, string, logical, bit += -= *= none /= %= **=
.= none
&= |= none
<<= >>= &= |= ^=
+= -= *= none /= %= **=
.= x=
&&= ||= ^=
<<= >>= &= |= ^=
# do not return values:
+= -= *= /= //= %= **=
+= *=
&= |= ^=
<<= >>= &= |= ^=
+= -= *= /= none %= **=
+= *=
&&= ||= ^=
<<= >>= &= |= ^=
increment and decrement $x = 1; ++$x;
–$x;$x = 1;
++$x; –$x;
none x = 1
# x not mutated:
x.succ
x.pred
local variable declarations # in function body:
$v = NULL;$a = array();
$d = array();$x = 1;
list($y,$z) = array(2, 3);
my $v; my (@a, %d); my$x = 1;
my ($y,$z) = (2, 3);
# in function body:
v = None
a, d = [], {}
x = 1
y, z = 2, 3
v = nil
a, d = [], {}
x = 1
y, z = 2, 3
regions which define local scope top level:
function or method body

nestable (with use clause):
anonymous function body

top level:
file

nestable:
function body
anonymous function body
anonymous block

function or method body
top level:
file
class block
module block
method body

nestable:
anonymous function block
anonymous block

global variable list($g1,$g2) = array(7, 8);
function swap_globals() {
global $g1,$g2;
list($g1,$g2) = array($g2,$g1);
}
our ($g1,$g2) = (7, 8);
sub swap_globals() {
($g1,$g2) = ($g2,$g1);
}
g1, g2 = 7, 8
def swap_globals():
global g1, g2
g1, g2 = g2, g1
$g1,$g2 = 7, 8
def swap_globals()
$g1,$g2 = $g2,$g1
end
constant declaration define(“PI”, 3.14); use constant PI => 3.14; # uppercase identifiers
# constant by convention
PI = 3.14
# warning if capitalized
# identifier is reassigned
PI = 3.14
to-end-of-line comment // comment
# comment
# comment # comment # comment
comment out multiple lines /* comment line
another line */
=for
comment line
another line
=cut
use triple quote string literal:
”’comment line
another line”’
=begin
comment line
another line
=end
null NULL # case insensitive undef None nil
null test is_null($v) ! isset($v)
! defined $v v == None v is None v == nil v.nil? undefined variable access NULL error under use strict; otherwise undef raises NameError raises NameError undefined test same as null test; no distinction between undefined variables and variables set to NULL same as null test; no distinction between undefined variables and variables set to undef not_defined = False try: v except NameError: not_defined = True ! defined?(v) arithmetic and logic php perl python ruby true and false TRUE FALSE # case insensitve 1 ”" True False true false falsehoods FALSE NULL 0 0.0 ”" “0″ array() undef 0 0.0 ”" “0″ () False None 0 0.0 ” [] {} false nil logical operators && || ! lower precedence: and or xor && || ! lower precedence: and or xor not and or not && || ! lower precedence: and or not conditional expression$x > 0 ? $x : -$x $x > 0 ?$x : -$x x if x > 0 else -x x > 0 ? x : -x comparison operators == != or <> > < >= <= no conversion: === !== numbers only: == != > < >= <= strings: eq ne gt lt ge le == != > < >= <= == != > < >= <= three value comparison none 0 <=> 1 “do” cmp ”re” removed from Python 3: cmp(0, 1) cmp(‘do’, ’re’) 0 <=> 1 “do” <=> ”re” convert from string, to string 7 + ”12″ 73.9 + ”.037″ “value: “ . 8 7 + ”12″ 73.9 + ”.037″ “value: “ . 8 7 + int(’12′) 73.9 + float(‘.037′) ‘value: ‘ + str(8) 7 + ”12″.to_i 73.9 + ”.037″.to_f “value: “ + “8″.to_s arithmetic operators + – * / none % pow(b,e) + – * / none % ** + – * / // % ** + – * x.fdiv(y) / % ** integer division and divmod (int) (13 / 5) none int ( 13 / 5 ) none 13 // 5 q, r = divmod(13, 5) 13 / 5 q, r = 13.divmod(5) float division 13 / 5 13 / 5 float(13) / 5 # Python 3: 13 / 5 13.to_f / 5 or 13.fdiv(5) arithmetic functions sqrt exp log sin cos tan asin acos atan atan2 use Math::Trig qw( tan asin acos atan); sqrt exp log sin cos tan asin acos atan atan2 from math import sqrt, exp, log, \ sin, cos, tan, asin, acos, atan, atan2 include Math sqrt exp log sin cos tan asin acos atan atan2 arithmetic truncation (int)$x
round($x) ceil($x)
floor($x) abs($x)
# cpan -i Number::Format
use Number::Format ’round’;
use POSIX qw(ceil floor);

int($x) round($x, 0)
ceil($x) floor($x)
abs($x) import math int(x) int(round(x)) math.ceil(x) math.floor(x) abs(x) x.to_i x.round x.ceil x.floor x.abs min and max min(1,2,3) max(1,2,3)$a = array(1,2,3)
min($a) max($a)
use List::Util qw(min max);

min(1,2,3);
max(1,2,3);
@a = (1,2,3);
min(@a);
max(@a);

min(1,2,3)
max(1,2,3)
min([1,2,3])
max([1,2,3])
[1,2,3].min
[1,2,3].max
division by zero returns FALSE with warning error raises ZeroDivisionError integer division raises ZeroDivisionError
float division returns Infinity
integer overflow converted to float converted to float; use Math::BigInt to create arbitrary length integers becomes arbitrary length integer of type long becomes arbitrary length integer of type Bignum
float overflow INF inf raises OverflowError Infinity
sqrt -2 NaN error unless use Math::Complex in effect # raises ValueError:
import math
math.sqrt(-2)

# returns complex float:
import cmath
cmath.sqrt(-2)

raises Errno::EDOM
rational numbers none use Math::BigRat;

my $x = Math::BigRat->new(“22/7″);$x->numerator();
$x->denominator(); from fractions import Fraction x = Fraction(22,7) x.numerator x.denominator require ’rational’ x = Rational(22,7) x.numerator x.denominator complex numbers none use Math::Complex; my$z = 1 + 1.414 * i;
Re($z); Im($z);

z = 1 + 1.414j
z.real
z.imag
require ’complex’

z = 1 + 1.414.im
z.real
z.imag

random integer, uniform float, normal float rand(0,99)
lcg_value()
none
int(rand() * 100)
rand()
none
import random

random.randint(0,99)
random.random()
random.gauss(0,1)

rand(100)
rand
none
set random seed, get and restore seed srand(17);

none

srand 17;

my $sd = srand; srand($sd);

import random

random.seed(17)
sd = random.getstate()
random.setstate(sd)

srand(17)

sd = srand
srand(sd)

bit operators << >> & | ^ ~ << >> & | ^ ~ << >> & | ^ ~ << >> & | ^ ~
binary, octal, and hex literals none
052
0x2a
0b101010
052
0x2a
0b101010
052
0x2a
0b101010
052
0x2a
base conversion base_convert(“42″, 10, 7);
base_convert(“60″, 7, 10);
# cpan -i Math::BaseCalc
use Math::BaseCalc;

$c = new Math::BaseCalc(digits=> [0..6]);$c->to_base(42);
$c->from_base(“60″); none int(“60″, 7) 42.to_s(7) “60″.to_i(7) strings php perl python ruby string literal “don’t say \”no\”" ‘don\’t say “no”‘ “don’t say \”no\”" ‘don\’t say “no”‘ ‘don\’t say “no”‘ “don’t say \”no\”" r”don’t “ r’say “no”‘ “don’t say \”no\”" ‘don\’t say “no”‘ newline in literal yes yes no, use escape or triple quote literal yes backslash escapes double quoted: \f \n \r \t \v \xhh \$ \” \ooo

single quoted:
\’ \\

double quoted:
\a \b \cx \e \f \n \r \t \xhh \x{hhhh} \ooo

single quoted:
\’ \\

single and double quoted:
\newline \\ \’ \” \a \b \f \n \r \t \v \ooo \xhh

Python 3:
\uhhhh \Uhhhhhhhh

double quoted:
\a \b \cx \e \f \n \r \s \t \uhhhh \u{hhhhh} \v \xhh \ooo

single quoted:
\’ \\

variable interpolation $count = 3;$item = ”ball”;
echo ”$count${item}s\n”;
my $count = 3; my$item = ”ball”;
print ”$count${item}s\n”;
none count = 3
item = ”ball”
puts ”#{count} #{item}s”
custom delimiters none my $s1 = q(lorem ipsum); my$s2 = qq($s1 dolor sit amet); none s1 = %q(lorem ipsum) s2 = %Q(#{s1} dolor sit amet) sprintf$fmt = ”lorem %s %d %f”;
sprintf($fmt, ”ipsum”, 13, 3.7); my$fmt = ”lorem %s %d %f”;
sprintf($fmt, ”ipsum”, 13, 3.7) ‘lorem %s %d %f’ % (‘ipsum’,13,3.7) fmt = ’lorem {0} {1} {2}’ str.format(fmt, ’ipsum’, 13, 3.7) “lorem %s %d %f” % ["ipsum",13,3.7] here document$word = ”amet”;
$s = <<<EOF lorem ipsum dolor sit$word
EOF;
$word = ”amet”;$s = <<EOF;
lorem ipsum
dolor sit $word EOF none word = ”amet” s = <<EOF lorem ipsum dolor sit #{word} EOF concatenate$s = ”Hello, “;
$s2 =$s . ”World!”;
my $s = ”Hello, “; my$s2 = $s . ”World!”; s = ’Hello, ‘ s2 = s + ’World!’ juxtaposition can be used to concatenate literals: s2 = ’Hello, ‘ “World!” s = ”Hello, ” s2 = s + ”World!” juxtaposition can be used to concatenate literals: s2 =”Hello, ” ‘World!’ replicate$hbar = str_repeat(“-”, 80); my $hbar = ”-” x 80; hbar = ’-' * 80 hbar = ”-” * 80 split, split in two, split into characters explode(” “, ”do re mi fa”) preg_split(‘/\s+/’, ”do re mi fa”, 2) preg_split(‘//’, ”abcd”, -1, PREG_SPLIT_NO_EMPTY) split(/\s+/, ”do re mi fa”) split(/\s+/, ”do re mi fa”, 2) split(//, ”abcd”) ‘do re mi fa’.split() re.split(‘\s+’, ’do re mi fa’, 1) list(‘abcd’) “do re mi fa”.split “do re mi fa”.split(/\s+/, 2) “abcd”.split(“”) join$a = array(“do”, ”re”, ”mi”, ”fa”);
implode(” “, $a) join(” “, qw(do re mi fa)) ‘ ‘.join(['do', 're', 'mi', 'fa']) %w(do re mi fa).join(‘ ‘) case manipulation strtoupper(“lorem”) strtolower(“LOREM”) ucfirst(“lorem”) uc(“lorem”) lc(“LOREM”) ucfirst(“lorem”) ‘lorem’.upper() ‘LOREM’.lower() ‘lorem’.capitalize() “lorem”.upcase “LOREM”.downcase “lorem”.capitalize strip trim(” lorem “) ltrim(” lorem”) rtrim(“lorem “) # cpan -i Text::Trim use Text::Trim; trim ” lorem ” ltrim ” lorem” rtrim ”lorem “ ‘ lorem ‘.strip() ‘ lorem’.lstrip() ‘lorem ‘.rstrip() ” lorem “.strip ” lorem”.lstrip “lorem “.rstrip pad on right, on left str_pad(“lorem”, 10) str_pad(“lorem”, 10, ” “, STR_PAD_LEFT) sprintf(“%-10s”, ”lorem”) sprintf(“%10s”, ”lorem”) ‘lorem’.ljust(10) ‘lorem’.rjust(10) “lorem”.ljust(10) “lorem”.rjust(10) length strlen(“lorem”) length(“lorem”) len(‘lorem’) “lorem”.length “lorem”.size index of substring strpos(“lorem ipsum”, ”ipsum”) returns FALSE if not found index(“lorem ipsum”, ”ipsum”) returns -1 if not found ‘lorem ipsum’.index(‘ipsum’) raises ValueError if not found “lorem ipsum”.index(“ipsum”) returns nil if not found extract substring substr(“lorem ipsum”, 6, 5) substr(“lorem ipsum”, 6, 5) ‘lorem ipsum’[6:11] “lorem ipsum”[6, 5] extract character syntax error to use index notation directly on string literal:$s = ”lorem ipsum”;
$s[6]; can’t use index notation with strings: substr(“lorem ipsum”, 6, 1) ‘lorem ipsum’[6] “lorem ipsum”[6] chr and ord chr(65) ord(“A”) chr(65) ord(“A”) chr(65) ord(‘A’) 65.chr “A”.ord character translation$ins = implode(range(“a”, ”z”));
$outs = substr($ins, 13, 13) . substr($ins, 0, 13); strtr(“hello”,$ins, $outs)$s = ”hello”;
$s =~ tr/a-z/n-za-m/; from string import lowercase as ins from string import maketrans outs = ins[13:] + ins[:13] ‘hello’.translate(maketrans(ins,outs)) “hello”.tr(“a-z”, ”n-za-m”) regular expresions php perl python ruby literal, custom delimited literal ‘/lorem|ipsum/’ ‘(/etc/hosts)’ /lorem|ipsum/ qr(/etc/hosts) re.compile(‘lorem|ipsum’) none /lorem|ipsum/ %r(/etc/hosts) character class abbreviations and anchors char class abbrevs: . \d \D \h \H \s \S \v \V \w \W anchors: ^$ \A \b \B \z \Z

char class abbrevs:
. \d \D \h \H \s \S \v \V \w \W

anchors: ^ $\A \b \B \z \Z char class abbrevs: . \d \D \s \S \w \W anchors: ^$ \A \b \B \Z

char class abbrevs:
. \d \D \h \H \s \S \w \W

anchors: ^ $\A \b \B \z \Z match test if (preg_match(‘/1999/’,$s)) {
echo ”party!\n”;
}
if ($s ~~ /1999/) { print ”party!\n”; } if re.search(’1999′, s): print(‘party!’) if /1999/.match(s) puts ”party!” end case insensitive match test preg_match(‘/lorem/i’, ”Lorem”) “Lorem” ~~ /lorem/i re.search(‘lorem’, ’Lorem’, re.I) /lorem/i.match(“Lorem”) modifiers e i m s x i m s p x re.I re.M re.S re.X i o m x substitution$s = ”do re mi mi mi”;
$s = preg_replace(‘/mi/’, ”ma”,$s);
my $s = ”do re mi mi mi”;$s =~ s/mi/ma/g;
s = ’do re mi mi mi’
s = re.compile(‘mi’).sub(‘ma’, s)
s = ”do re mi mi mi”
s.gsub!(/mi/, ”ma”)
match, prematch, postmatch none if ($s =~ /\d{4}/p) {$match = ${^MATCH};$prematch = ${^PREMATCH};$postmatch = ${^POSTMATCH}; } m = re.search(‘\d{4}’, s) if m: match = m.group() prematch = s[0:m.start(0)] postmatch = s[m.end(0):len(s)] m = /\d{4}/.match(s) if m match = m[0] prematch = m.pre_match postmatch = m.post_match end group capture$s = ”2010-06-03″;
$rx = ’/(\d{4})-(\d{2})-(\d{2})/’; preg_match($rx, $s,$m);
list($_,$yr, $mo,$dy) = $m;$rx = qr/(\d{4})-(\d{2})-(\d{2})/;
“2010-06-03″ =~ $rx; ($yr, $mo,$dy) = ($1,$2, $3); rx = ’(\d{4})-(\d{2})-(\d{2})’ m = re.search(rx, ’2010-06-03′) yr, mo, dy = m.groups() rx = /(\d{4})-(\d{2})-(\d{2})/ m = rx.match(“2010-06-03″) yr, mo, dy = m[1..3] scan$s = ”dolor sit amet”;
preg_match_all(‘/\w+/’, $s,$m);
$a =$m[0];
my $s = ”dolor sit amet”; @a =$s =~ m/\w+/g;
s = ’dolor sit amet’
a = re.findall(‘\w+’, s)
a = ”dolor sit amet”.scan(/\w+/)
backreference in match and substitution preg_match(‘/(\w+) \1/’, ”do do”)

$s = ”do re”;$rx = ’/(\w+) (\w+)/’;
$s = preg_replace($rx, ’\2 \1′, $s); “do do” =~ /(\w+) \1/ my$s = ”do re”;
$s =~ s/(\w+) (\w+)/$2 $1/; none rx = re.compile(‘(\w+) (\w+)’) rx.sub(r’\2 \1′, ’do re’) /(\w+) \1/.match(“do do”) “do re”.sub(/(\w+) (\w+)/, ’\2 \1′) recursive regex ‘/$$([^()]*|(R))$$/’ /$$([^()]*|(?R))$$/ none Ruby 1.9: /(?<p>$$([^()]*|\g<p>)*$$)/ dates and time php perl python ruby date/time type DateTime Time::Piece if use Time::Piece in effect, otherwise tm array datetime.datetime Time current date/time$t = new DateTime(“now”);
$utc_tmz = new DateTimeZone(“UTC”);$utc = new DateTime(“now”, $utc_tmz); use Time::Piece; my$t = localtime(time);
my $utc = gmtime(time); import datetime t = datetime.datetime.now() utc = datetime.datetime.utcnow() t = Time.now utc = Time.now.utc to unix epoch, from unix epoch$epoch = $t->getTimestamp();$t2 = new DateTime();
$t2->setTimestamp(1304442000); use Time::Local; use Time::Piece; my$epoch = timelocal($t); my$t2 = localtime(1304442000);

from datetime import datetime as dt

epoch = int(t.strftime(“%s”))
t2 = dt.fromtimestamp(1304442000)

epoch = t.to_i
t2 = Time.at(1304442000)
current unix epoch $epoch = time();$epoch = time; convert datetime.datetime.now() convert Time.now
strftime strftime(“%Y-%m-%d %H:%M:%S”, $epoch); date(“Y-m-d H:i:s”,$epoch);
$t->format(“Y-m-d H:i:s”); use Time::Piece;$t = localtime(time);
$fmt = ”%Y-%m-%d %H:%M:%S”; print$t->strftime($fmt); t.strftime(‘%Y-%m-%d %H:%M:%S’) t.strftime(“%Y-%m-%d %H:%M:%S”) default format example no default string representation Tue Aug 23 19:35:19 2011 2011-08-23 19:35:59.411135 2011-08-23 17:44:53 -0700 strptime$fmt = ”Y-m-d H:i:s”;
$s = ”2011-05-03 10:00:00″;$t = DateTime::createFromFormat($fmt,$s);
use Time::Local;
use Time::Piece;

$s = ”2011-05-03 10:00:00″;$fmt = ”%Y-%m-%d %H:%M:%S”;
$t = Time::Piece->strptime($s,$fmt); from datetime import datetime s = ’2011-05-03 10:00:00′ fmt = ’%Y-%m-%d %H:%M:%S’ t = datetime.stptime(s, fmt) require ’date’ s = ”2011-05-03 10:00:00″ fmt = ”%Y-%m-%d %H:%M:%S” t = Date.strptime(s, fmt).to_time parse date w/o format$epoch = strtotime(“July 7, 1999″); # cpan -i Date::Parse
use Date::Parse;

$epoch = str2time(“July 7, 1999″); # pip install python-dateutil import dateutil.parser s = ’July 7, 1999′ t = dateutil.parser.parse(s) require ’date’ s = ”July 7, 1999″ t = Date.parse(s).to_time result of date subtraction DateInterval object if diff method used:$fmt = ”Y-m-d H:i:s”;
$s = ”2011-05-03 10:00:00″;$then = DateTime::createFromFormat($fmt,$s);
$now = new DateTime(“now”);$interval = $now->diff($then);
Time::Seconds object if use Time::Piece in effect; not meaningful to subtract tm arrays datetime.timedelta //object Float containing time difference in seconds
add time duration $now = new DateTime(“now”);$now->add(new DateInterval(“PT10M3S”);
use Time::Seconds;

$now = localtime(time);$now += 10 * ONE_MINUTE() + 3;

import datetime

delta = datetime.timedelta(
minutes=10,
seconds=3)
t = datetime.datetime.now() + delta

require ’date/delta’

s = ”10 min, 3 s”
delta = Date::Delta.parse(s).in_secs
t = Time.now + delta

local timezone DateTime objects can be instantiated without specifying the timezone if a default is set:
$s = ”America/Los_Angeles”; date_default_timezone_set($s);
Time::Piece has local timezone if created with localtimeand UTC timezone if created with gmtime; tm arrays have no timezone or offset info a datetime object has no timezone information unless atzinfo object is provided when it is created if no timezone is specified the local timezone is used
timezone name; offset from UTC; is daylight savings? $tmz = date_timezone_get($t);
timezone_name_get($tmz); date_offset_get($t) / 3600;
$t->format(“I”); # cpan -i DateTime use DateTime; use DateTime::TimeZone;$dt = DateTime->now();
$tz = DateTime::TimeZone->new( name=>”local”);$tz->name;
$tz->offset_for_datetime($dt) /
3600;
$tz->is_dst_for_datetime($dt);

import time

tm = time.localtime()

time.tzname[tm.tm_isdst]
(time.timezone / -3600) + tm.tm_isdst
tm.tm_isdst

t.zone
t.utc_offset / 3600
t.dst?
microseconds list($frac,$sec) = explode(” “,
microtime());
$usec =$frac * 1000 * 1000;
use Time::HiRes qw(gettimeofday);

($sec,$usec) = gettimeofday;

t.microsecond t.usec
sleep a float argument will be truncated to an integer:
sleep(1);
a float argument will be truncated to an integer:
sleep 1;
import time

time.sleep(0.5)

sleep(0.5)
timeout use set_time_limit to limit execution time of the entire script; use stream_set_timeout to limit time spent reading from a stream opened with fopen or fsockopen eval {
$SIG{ALRM}= sub {die ”timeout!”;}; alarm 5; sleep 10; }; alarm 0; import signal, time class Timeout(Exception): pass def timeout_handler(signo, fm): raise Timeout() signal.signal(signal.SIGALRM, timeout_handler) try: signal.alarm(5) time.sleep(10) except Timeout: pass signal.alarm(0) require ’timeout’ begin Timeout.timeout(5) do sleep(10) end rescue Timeout::Error end arrays php perl python ruby literal$a = array(1, 2, 3, 4); @a = (1, 2, 3, 4); a = [1, 2, 3, 4] a = [1, 2, 3, 4]
quote words none @a = qw(do re mi); none a = %w(do re mi)
size count($a)$#a + 1 or
scalar(@a)
len(a) a.size
a.length # same as size
empty test !$a !@a not a NoMethodError if a is nil: a.empty? lookup$a[0] $a[0] a[0] a[0] update$a[0] = ”lorem”; $a[0] = ”lorem”; a[0] = ’lorem’ a[0] = ”lorem” out-of-bounds behavior$a = array();
evaluates as NULL:
$a[10]; increases array size to one:$a[10] = ”lorem”;
@a = ();
evaluates as undef:
$a[10]; increases array size to 11:$a[10] = ”lorem”;
a = []
raises IndexError:
a[10]
raises IndexError:
a[10] = ’lorem’
a = []
evaluates as nil:
a[10]
increases array size to 11:
a[10] = ”lorem”
index of array element $a = array(“x”, ”y”, ”z”, ”w”);$i = array_search(“y”, $a); use List::Util ’first’; @a = qw(x y z w);$i = first {$a[$_] eq ”y”} (0..$#a); a = ['x', 'y', 'z', 'w'] i = a.index(‘y’) a = %w(x y z w) i = a.index(“y”) slice by endpoints, by length select 3rd and 4th elements: none array_slice($a, 2, 2)
select 3rd and 4th elements:
@a[2..3]
splice(@a, 2, 2)
select 3rd and 4th elements:
a[2:4]
none
select 3rd and 4th elements:
a[2..3]
a[2, 2]
slice to end array_slice($a, 1) @a[1..$#a] a[1:] a[1..-1]
manipulate back $a = array(6,7,8); array_push($a, 9);
$a[] = 9; # same as array_push array_pop($a);
@a = (6,7,8);
push @a, 9;
pop @a;
a = [6,7,8]
a.append(9)
a.pop()
a = [6,7,8]
a.push(9)
a << 9 # same as push
a.pop
manipulate front $a = array(6,7,8); array_unshift($a, 5);
array_shift($a); @a = (6,7,8); unshift @a, 5; shift @a; a = [6,7,8] a.insert(0,5) a.pop(0) a = [6,7,8] a.unshift(5) a.shift concatenate$a = array(1,2,3);
$a2 = array_merge($a,array(4,5,6));
$a = array_merge($a,array(4,5,6));
@a = (1,2,3);
@a2 = (@a,(4,5,6));
push @a, (4,5,6);
a = [1,2,3]
a2 = a + [4,5,6]
a.extend([4,5,6])
a = [1,2,3]
a2 = a + [4,5,6]
a.concat([4,5,6])
address copy, shallow copy, deep copy $a = array(1,2,array(3,4));$a2 =& $a; none$a4 = $a; use Storable ’dclone’ my @a = (1,2,[3,4]); my$a2 = \@a;
my @a3 = @a;
my @a4 = @{dclone(\@a)};

import copy

a = [1,2,[3,4]]
a2 = a
a3 = list(a)
a4 = copy.deepcopy(a)

a = [1,2,[3,4]]
a2 = a
a3 = a.dup
arrays as function arguments parameter contains deep copy each element passed as separate argument; use reference to pass array as single argument parameter contains address copy parameter contains address copy
iteration foreach (array(1,2,3) as $i) { echo ”$i\n”;
}
for $i (1 2 3) { print ”$i\n” } for i in [1,2,3]:
print(i)
[1,2,3].each { |i| puts i }
indexed iteration $a = array(“do”, ”re”, ”mi” ”fa”); foreach ($a as $i =>$s) {
echo ”$s at index$i\n”;
}
none; use range iteration from 0 to $#a and use index to look up value in the loop body a = ['do', 're', 'mi', 'fa'] for i, s in enumerate(a): print(‘%s at index %d’ % (s, i)) a = %w(do re mi fa) a.each_with_index do |s,i| puts ”#{s} at index #{i}” end iterate over range not space efficient; use C-style for loop for$i (1..1_000_000) {
code
}
range replaces xrange in Python 3:
for i in xrange(1, 1000001):
code
(1..1_000_000).each do |i|
code
end
instantiate range as array $a = range(1, 10); @a = 1..10; a = range(1, 11) Python 3: a = list(range(1, 11)) a = (1..10).to_a reverse$a = array(1,2,3);
array_reverse($a);$a = array_reverse($a); @a = (1,2,3); reverse @a; @a = reverse @a; a = [1,2,3] a[::-1] a.reverse() a = [1,2,3] a.reverse a.reverse! sort$a = array(“b”, ”A”, ”a”, ”B”);
none
sort($a); none, but usort sorts in place @a = qw(b A a B); sort @a; @a = sort @a; sort { lc($a) cmp lc($b) } @a; a = ['b', 'A', 'a', 'B'] sorted(a) a.sort() a.sort(key=str.lower) a = %w(b A a B) a.sort a.sort! a.sort do |x,y| x.downcase <=> y.downcase end dedupe$a = array(1,2,2,3);
$a2 = array_unique($a);
$a = array_unique($a);
use List::MoreUtils ’uniq’;

my @a = (1,2,2,3);
my @a2 = uniq @a;
@a = uniq @a;

a = [1,2,2,3]
a2 = list(set(a))
a = list(set(a))
a = [1,2,2,3]
a2 = a.uniq
a.uniq!
membership in_array(7, $a) 7 ~~ @a 7 in a a.include?(7) intersection$a = array(1,2);
$b = array(2,3,4) array_intersect($a, $b) set.intersection({1,2}, {2,3,4}) [1,2] & [2,3,4] union$a1 = array(1,2);
$a2 = array(2,3,4); array_unique(array_merge($a1, $a2)) set.union({1,2}, {2,3,4}) [1,2] | [2,3,4] set difference$a1 = array(1,2,3);
$a2 = array(2); array_values(array_diff($a1, $a2)) {1,2,3} – {2} [1,2,3] – [2] map array_map(function ($x) {
return $x*$x;
}, array(1,2,3))
map { $_ *$_ } (1,2,3) map(lambda x: x * x, [1,2,3])
# or use list comprehension:
[x*x for x in [1,2,3]]
[1,2,3].map { |o| o*o }
filter array_filter(array(1,2,3),
function ($x) { return$x>1;
})
grep { $_ > 1 } (1,2,3) filter(lambda x: x > 1, [1,2,3]) # or use list comprehension: [x for x in [1,2,3] if x > 1] [1,2,3].select { |o| o > 1 } reduce array_reduce(array(1,2,3), function($x,$y) { return$x+$y; }, 0) use List::Util ’reduce’; reduce {$x + $y } 0, (1,2,3) # import needed in Python 3 only import reduce from functools reduce(lambda x, y: x+y, [1,2,3], 0) [1,2,3].inject(0) { |m,o| m+o } universal and existential tests use array_filter # cpan -i List::MoreUtils use List::MoreUtils qw(all any); all {$_ % 2 == 0 } (1,2,3,4)
any { $_ % 2 == 0 } (1,2,3,4) all(i%2 == 0 for i in [1,2,3,4]) any(i%2 == 0 for i in [1,2,3,4]) [1,2,3,4].all? {|i| i.even? } [1,2,3,4].any? {|i| i.even? } shuffle and sample$a = array(1, 2, 3, 4);
shuffle($a); array_rand($a, 2)
use List::Util ’shuffle’;

@a = (1, 2, 3, 4);
shuffle(@a);
none

from random import shuffle, sample

a = [1, 2, 3, 4]
shuffle(a)
sample(a, 2)

[1, 2, 3, 4].shuffle
Ruby 1.9:
[1, 2, 3, 4].sample(2)
zip # array of 3 pairs:
$a = array_map(NULL, array(1, 2, 3), array(“a”, ”b”, ”c”)); # cpan -i List::MoreUtils use List::MoreUtils ’zip’; @nums = (1, 2, 3); @lets = qw(a b c); # flat array of 6 elements: @a = zip @nums, @lets; # array of 3 pairs: a = zip([1,2,3], ['a', 'b', 'c']) # array of 3 pairs: a = [1,2,3].zip(["a", "b", "c"]) dictionaries php perl python ruby literal$d = array(“t” => 1, ”f” => 0); %d = ( t => 1, f => 0 ); d = { ’t':1, ’f':0 } d = { ”t” => 1, ”f” => 0 }
size count($d) scalar(keys %d) len(d) d.size d.length # same as size lookup$d["t"] $d{“t”} d['t'] d["t"] out-of-bounds behavior$d = array();
evaluates as NULL:
$d["lorem"]; adds key/value pair:$d["lorem"] = ”ipsum”;
%d = ();
evaluates as undef:
$d{“lorem”}; adds key/value pair:$d{“lorem”} = ”ipsum”;
d = {}
raises KeyError:
d['lorem']
d['lorem'] = ’ipsum’
d = {}
evaluates as nil:
d["lorem"]
d["lorem"] = ”ipsum”
is key present array_key_exists(“y”, $d); exists$d{“y”} ‘y’ in d d.has_key?(“y”)
delete entry $d = array(1 => ”t”, 0 => ”f”); unset($d[1]);
%d = ( 1 => ”t”, 0 => ”f” );
delete $d{1}; d = {1: True, 0: False} del d[1] d = {1 => true, 0 => false} d.delete(1) from array of pairs, from even length array @a = (1,”a”,2,”b”,3,”c”); %d = @a; a = [[1,'a'], [2,'b'], [3,'c']] d = dict(a) a = [1,'a',2,'b',3,'c'] d = dict(zip(a[::2], a[1::2])) a = [[1,"a"], [2,"b"], [3,"c"]] d = Hash[a] a = [1,"a",2,"b",3,"c"] d = Hash[*a] merge$d1 = array(“a”=>1, ”b”=>2);
$d2 = array(“b”=>3, ”c”=>4);$d1 = array_merge($d1,$d2);
%d1 = (a=>1, b=>2);
%d2 = (b=>3, c=>4);
%d1 = (%d1, %d2);
d1 = {‘a’:1, ’b':2}
d2 = {‘b’:3, ’c':4}
d1.update(d2)
d1 = {“a”=>1, ”b”=>2}
d2 = {“b”=>3, ”c”=>4}
d1.merge!(d2)
invert $to_num = array(“t”=>1, ”f”=>0);$to_let = array_flip($to_num); %to_num = (t=>1, f=>0); %to_let = reverse %to_num; to_num = {‘t’:1, ’f':0} a = [[v,k] for k,v in to_num.items()] to_let = dict(a) to_num = {“t”=>1, ”f”=>0} to_let = to_num.invert iteration foreach ($d as $k =>$v ) { while ( ($k,$v) = each %d ) { for k, v in d.iteritems():
code
d.each { |k,v| code }
keys and values as arrays array_keys($d) array_values($d)
keys %d
values %d
d.keys()
d.values()
d.keys
d.values
set default value class CountArray extends ArrayObject {
public function offsetExists($i) { return true; } public function offsetGet($i) {
if(!parent::offsetExists($i)) { parent::offsetSet($i, 0);
}
return parent::offsetGet($i); } }$counts = new CountArray();
define a tied hash class CountDict(dict):
def __missing__(self, k):
return 0

counts = CountDict()

counts = Hash.new do |h,k|
h[k] = 0
end
functions
php perl python ruby
function declaration function add($a,$b) {
return $a +$b;
}
sub add { $_[0] +$_[1] } def add(a,b):
return a+b
a+b
end
function invocation function names are case insensitive:
missing argument behavior set to NULL with warning set to undef raises TypeError raises ArgumentError
default value function my_log($x,$base=10) { sub my_log {
my $x = shift; my$base = shift // 10;
def log(x, base=10): def log(x, base=10)
arbitrary number of arguments function add() {
return array_sum(func_get_args());
}
@_ contains all values def add(first,*rest):
if not rest:
return first
else:
if rest.empty?
first
else
end
end
named parameter definition none none def foo(**d): def foo(h)
named parameter invocation none none foo(eps=0.01) foo(:eps => 0.01)
pass number or string by reference function foo(&$x, &$y) {
$x += 1;$y .= ”ly”;
}

$n = 7;$s = ”hard”;
foo($n,$s);

sub foo {
$_[0] += 1;$_[1] .= ”ly”;
}

my $n = 7; my$s = ”hard”;
foo($n,$s);

not possible not possible
pass array or dictionary by reference function foo(&$x, &$y) {
$x[2] = 5;$y["f"] = -1;
}

$a = array(1,2,3);$d = array(“t”=>1,”f”=>0);
foo($a,$d);

sub foo {
$_[0][2] = 5;$_[1]{“f”} = -1;
}

my @a = (1,2,3);
my %d = (“t”=> 1, ”f” => 0);
foo(\@a, \%d);

def foo(x, y):
x[2] = 5
y['f'] = -1

a = [1,2,3]
d = {‘t’:1, ’f':0}
foo(a, d)

def foo(x, y)
x[2] = 5
y["f"] = -1
end

a = [1,2,3]
d = {“t”=> 1, ”f” => 0 }
foo(a, d)

return value return arg or NULL return arg or last expression evaluated return arg or None return arg or last expression evaluated
multiple return values function first_and_second(&$a) { return array($a[0], $a[1]); }$a = array(1,2,3);
list($x,$y) =
first_and_second($a); sub first_and_second { return ($_[0], $_[1]); } @a = (1,2,3); ($x, $y) = first_and_second(@a); def first_and_second(a): return a[0], a[1] x, y = first_and_second([1,2,3]) def first_and_second(a) return a[0], a[1] end x, y = first_and_second([1,2,3]) lambda declaration$sqr = function ($x) { return$x * $x; };$sqr = sub { $_[0] *$_[0] } sqr = lambda x: x * x sqr = lambda { |x| x * x }
lambda invocation $sqr(2)$sqr->(2) sqr(2) sqr.call(2) or
sqr[2]
function with private state function counter() {
static $i = 0; return ++$i;
}

echo counter();

use feature state;

sub counter {
state $i = 0; ++$i;
}

print counter() . ”\n”;

# state not private:
def counter():
counter.i += 1
return counter.i

counter.i = 0
print(counter())

none
closure function make_counter() {
$i = 0; return function () use (&$i) {
return ++$i; }; }$nays = make_counter();
echo $nays(); sub make_counter() { my$i = 0;
return sub() { ++$i }; } my$nays = make_counter();
print $nays->() . ”\n”; # Python 3: def make_counter(): i = 0 def counter(): nonlocal i i += 1 return i return counter nays = make_counter() def make_counter() i = 0 return lambda { i +=1; i } end nays = make_counter puts nays.call generator none none def make_counter(): i = 0 while True: i += 1 yield i nays = make_counter() print(nays.next()) # Ruby 1.9: def make_counter() return Fiber.new do i = 0 while true i += 1 Fiber.yield i end end end nays = make_counter puts nays.resume execution control php perl python ruby if if ( 0 ==$n ) {
echo ”no hits\n”;
} elseif ( 1 == $n ) { echo ”one hit\n”; } else { echo ”$n hits\n”;
}
if ( 0 == $n ) { print ”no hits\n” } elsif ( 1 ==$n ) {
print ”one hit\n”
} else {
print ”$n hits\n” } if 0 == n: print(‘no hits’) elif 1 == n: print(‘one hit’) else: print(str(n) + ’ hits’) if n == 0 puts ”no hits” elsif 1 == n puts ”one hit” else puts ”#{n} hits” end switch switch ($n) {
case 0:
echo ”no hits\n”;
break;
case 1:
echo ”one hit\n”;
break;
default:
echo ”$n hits\n”; } use feature ’switch’; given ($n) {
when (0) { print ”no hits\n”; }
when (1) { print ”one hit\n”; }
default { print ”$n hits\n”; } } none case n when 0 puts ”no hits” when 1 puts ”one hit” else puts ”#{n} hits” end while while ($i < 100 ) { $i++; } while ($i < 100 ) { $i++ } while i < 100: i += 1 while i < 100 do i += 1 end c-style for for ($i = 1; $i <= 10;$i++) {
echo ”$i\n”; } for ($i=0; $i <= 10;$i++ ) {
print ”$i\n”; } none none break, continue, redo break continue none last next redo break continue none break next redo control structure keywords case default do else elseif for foreach goto if switch while do else elsif for foreach goto if unless until while elif else for if while case do else elsif end for loop when while unless until what do does starts body of a do-while loop executes following block and returns value of last statement executed raises NameError unless a value was assigned to it starts an anonymous block. Also starts the body of aloop, while, or until loop statement modifiers none print ”positive\n” if$i > 0;
print ”nonzero\n” unless $i == 0; none puts ”positive” if i > 0 puts ”nonzero” unless i == 0 raise exception throw new Exception(“bad arg”); die ”bad arg”; raise Exception(‘bad arg’) # raises RuntimeError raise ”bad arg” catch exception try { risky(); } catch (Exception$e) {
echo ”risky failed: “,
$e->getMessage(), ”\n”; } eval { risky }; if ($@) {
print ”risky failed: $@\n”; } try: risky() except: print(‘risky failed’) # catches StandardError begin risky rescue print ”risky failed: ” puts$!.message
end
global variable for last exception none $EVAL_ERROR:$@
$OS_ERROR:$!
$CHILD_ERROR:$?
none last exception: $! backtrace array of exc.:$@
exit status of child: $? define exception class Bam extends Exception { function __construct() { parent::__construct(“bam!”); } } none class Bam(Exception): def __init__(self): super(Bam, self).__init__(‘bam!’) class Bam < Exception def initialize super(“bam!”) end end catch exception by type try { throw new Bam; } catch (Bam$e) {
echo $e->getMessage(), ”\n”; } none try: raise Bam() except Bam as e: print(e) begin raise Bam.new rescue Bam => e puts e.message end finally/ensure none none acquire_resource() try: risky() finally: release_resource() acquire_resource begin risky ensure release_resource end start thread none use threads;$func = sub { sleep 10 };
$thr = threads->new($func);

def run(self):
time.sleep(10)

thr = sleep10()
thr.start()

thr = Thread.new { sleep 10 }
wait on thread none $thr->join; thr.join() thr.join files php perl python ruby print to standard output echo ”Hello, World!\n”; print ”Hello, World!\n”; print(‘Hello, World!’) puts ”Hello, World!” read from standard input$line = fgets(STDIN); $line = <>; line = sys.stdin.readline() line = gets standard file handles only set by CLI; not set when reading script from standard input: STDIN STDOUT STDERR STDIN STDOUT STDERR sys.stdin sys.stdout sys.stderr$stdin $stdout$stderr
open file $f = fopen(“/etc/hosts”, ”r”); open my$f, ”/etc/hosts”; or
open FILE, ”/etc/hosts”;
f = open(‘/etc/hosts’) f = File.open(“/etc/hosts”) or
File.open(“/etc/hosts”) { |f|
open file for writing $f = fopen(“/tmp/php_test”, ”w”); open my$f, ”>/tmp/perl_test”; or
open FILE, ”>/tmp/perl_test”;
f = open(‘/tmp/test’, ’w') f = File.open(‘/tmp/test’, ’w') or
File.open(‘/tmp/test’, ’w') { |f|
close file fclose($f); close$f; or
close FILE;
f.close() f.close
read line $line = fgets($f); $line = <$f>; or
$line = <FILE>; f.readline() f.gets iterate over file by line while (!feof($f)) {
$line = fgets($f);
while ($line = <$f>) { for line in f: f.each do |line|
chomp chop($line); chomp$line; line = line.rstrip(‘\r\n’) line.chomp!
read entire file into array or string $a = file(“/etc/hosts”);$s = file_get_contents(“/etc/hosts”);
@a = <$>;$s = do { local $/; <$f> };
a = f.lines.to_a
write to file fwrite($f, ”lorem ipsum”); print$f ”lorem ipsum”; f.write(‘lorem ipsum’) f.write(“lorem ipsum”)
flush file handle note that CLI output isn’t buffered
fflush($f); use IO::Handle;$f->flush();

f.flush() f.flush
file test, regular file test file_exists(“/etc/hosts”)
is_file(“/etc/hosts”)
-e ”/etc/hosts”
-f ”/etc/hosts”
os.path.exists(‘/etc/hosts’)
os.path.isfile(‘/etc/hosts’)
File.exists?(“/etc/hosts”)
File.file?(“/etc/hosts”)
copy file, remove file, rename file copy(“/tmp/foo”, ”/tmp/bar”);
rename(“/tmp/bar”, ”/tmp/foo”);
use File::Copy;

copy(“/tmp/foo”, ”/tmp/bar”);
move(“/tmp/bar”, ”/tmp/foo”);

import shutil

shutil.copy(‘/tmp/foo’, ’/tmp/bar’)
os.remove(‘/tmp/foo’)
shutil.move(‘/tmp/bar’, ’/tmp/foo’)

require ’fileutils’

FileUtils.cp(“/tmp/foo”, ”/tmp/bar”)
FileUtils.rm(“/tmp/foo”)
FileUtils.mv(“/tmp/bar”, ”/tmp/foo”)

set file permissions chmod(“/tmp/foo”, 0755); chmod 0755, ”/tmp/foo”; os.chmod(‘/tmp/foo’, 0755) File.chmod(0755, ”/tmp/foo”)
temporary file $tmp = tempnam(sys_get_temp_dir(), “foo”);$f = fopen($tmp, ”w”); fwrite($f, ”lorem ipsum\n”);
fclose($f); echo ”tmp file:$tmp\n”;

use File::Temp;

$f = File::Temp->new(); print$f ”lorem ipsum\n”;
close $f; print ”tmp file: “; print$f->filename . ”\n”;

import tempfile

f = tempfile.NamedTemporaryFile(
prefix=’foo’)
f.write(‘lorem ipsum\n’)
f.close()

print(“tmp file: %s” % f.name)

require ’tempfile’

f = Tempfile.new(‘foo’)
f.puts ”lorem ipsum”
f.close

puts ”tmp file: #{f.path}”

directories
php perl python ruby
build pathname “/etc” . DIRECTORY_SEPARATOR . ”hosts” use File::Spec;

File::Spec->catfile(“/etc”, ”hosts”)

os.path.join(‘/etc’, ’hosts’) File.join(“/etc”, ”hosts”)
dirname and basename dirname(“/etc/hosts”)
basename(“/etc/hosts”)
use File::Basename;

print dirname(“/etc/hosts”);
print basename(“/etc/hosts”);

os.path.dirname(‘/etc/hosts’)
os.path.basename(‘/etc/hosts’)
File.dirname(“/etc/hosts”)
File.basename(“/etc/hosts”)
iterate over directory by file if ($dir = opendir(“/etc”)) { while ($file = readdir($dir)) { echo ”$file\n”;
}
closedir($dir); } use File::Basename; while ( </etc/*> ) { print basename($_) . ”\n”;
}

for filename in os.listdir(‘/etc’):
print(filename)
Dir.open(“/etc”).each do |file|
puts file
end
make directory mkdir(“/tmp/foo/bar”, 0755, TRUE); use File::Path ’make_path’;

make_path ”/tmp/foo/bar”;

dirname = ’/tmp/foo/bar’
if not os.path.isdir(dirname):
os.makedirs(dirname)
require ’fileutils’

FileUtils.mkdir_p(“/tmp/foo/bar”)

recursive copy none # cpan -i File::Copy::Recursive
use File::Copy::Recursive ’dircopy’;

dircopy ”/tmp/foodir”,
“/tmp/bardir”;

import shutil

shutil.copytree(‘/tmp/foodir’,
‘/tmp/bardir’)

require ’fileutils’

FileUtils.cp_r(“/tmp/foodir”,
“/tmp/bardir”)

remove empty directory rmdir(“/tmp/foodir”); rmdir ”/tmp/foodir”; os.rmdir(‘/tmp/foodir’) File.rmdir(“/tmp/foodir”)
remove directory and contents none use File::Path ’remove_tree’;

remove_tree ”/tmp/foodir”;

import shutil

shutil.rmtree(‘/tmp/foodir’)

require ’fileutils’

FileUtils.rm_rf(“/tmp/foodir”)

directory test is_dir(“/tmp”) -d ”/tmp” os.path.isdir(‘/tmp’) File.directory?(“/tmp”)
processes and environment
php perl python ruby
command line args, script name count($argv)$argv[0] $argv[1] etc$_SERVER["SCRIPT_NAME"]
scalar(@ARGV)
$ARGV[0]$ARGV[1] etc
$0 len(sys.argv)-1 sys.argv[1] sys.argv[2] etc sys.argv[0] ARGV.size ARGV[0] ARGV[1] etc$0
environment variable getenv(“HOME”) $ENV{“HOME”} os.getenv(‘HOME’) ENV["HOME"] exit exit 0; exit 0; sys.exit(0) exit(0) set signal handller$SIG{INT} = sub {
die ”exiting…\n”;
};
import signal

def handler(signo, frame):
print(‘exiting…’)
exit -1
signal.signal(signal.SIGINT, handler)

Signal.trap(“INT”,
lambda do |signo|
puts ”exiting…”
exit
end
)
executable test is_executable(“/bin/ls”) -x ”/bin/ls” os.access(‘/bin/ls’, os.X_OK) File.executable?(“/bin/ls”)
external command exec(“ls”); system(“ls”); os.system(‘ls’) system(“ls”)
backticks $files = ls; my$files = ls; or
my $files = qx(ls); os.popen(‘ls’).read() files = ls or files = %x(ls) libraries and modules php perl python ruby load library require_once(“foo.php”); require ’Foo.pm’; # or require Foo; # or use Foo; import foo require ’foo’ # or require ’foo.rb’ reload library require(“foo.php”); do ’Foo.pm’; reload(foo) load ’foo.rb’ library path$o = ini_get(“include_path”);
$n =$o . ”:/some/path”;
ini_set(“include_path”, $n); push @INC, ”/some/path”; sys.path.append(‘/some/path’)$: << ”/some/path”
library path environment variable none PERL5LIB PYTHONPATH RUBYLIB
library path command line option none -I none -I
main in library unless (caller) {
code
}
if __name__ == ’__main__’:
code
if $0 == __FILE__ code end module declaration namespace Foo; package Foo; require Exporter; our @ISA = (“Exporter”); our @EXPORT_OK = qw(bar baz); put declarations in foo.py class Foo or module Foo submodule declaration namespace Foo\Bar; package Foo::Bar; create directory foo in library path containing file bar.py module Foo::Bar or module Foo module Bar module separator \Foo\Bar\baz(); Foo::Bar::baz(); foo.bar.baz() Foo::Bar.baz import all definitions in module none, but a long module name can be shortened # imports symbols in @EXPORT: use Foo; from foo import * include Foo import definitions only class names can be imported # bar and baz must be in # @EXPORT or @EXPORT_OK: use Foo qw(bar baz); from foo import bar, baz none managing multiple installations$ virtualenv -p /usr/bin/python foo
$source foo/bin/activate list installed packages, install a package$ pear list
$pear install Math_BigInteger$ perldoc perllocal
$cpan -i Moose$ pip freeze
$pip install jinja2$ gem list
$gem install rails objects php perl python ruby define class class Int { public$value;
function __construct($int=0) {$this->value = $int; } } package Int; use Moose; has value => (is => ”rw”); around BUILDARGS => sub { my$orig = shift;
my $class = shift; my$v = $_[0] || 0;$class->$orig(value =>$v);
};
no Moose;
class Int:
def __init__(self, v=0):
self.value = v
class Int
attr_accessor :value
def initialize(i=0)
@value = i
end
end
create object $i = new Int(); my$i = new Int(); # or
my $i = Int->new(); i = Int() i = Int.new get and set attribute$v = $i->value;$i->value = $v+1; my$v = $i->value;$i->value($v+1); v = i.value i.value = v+1 v = i.value i.value = v+1 instance variable accessibility must be declared private by default public; attributes starting with underscore private by convention private by default; use attr_reader, attr_writer, attr_accessor to make public define method function plus($i) {
return $this->value +$i;
}
# in package:
sub plus {
my $self = shift;$self->value + $_[0]; } def plus(self,v): return self.value + v def plus(i) value + i end invoke method$i->plus(7) $i->plus(7) i.plus(7) i.plus(7) destructor function __destruct() { echo ”bye,$this->value\n”;
}
# in package:
sub DEMOLISH {
my $self = shift; my$v = $self->value; print ”bye,$v\n”;
}
def __del__(self):
print(‘bye, %d’ % self.value)
val = i.value
ObjectSpace.define_finalizer(int) {
puts ”bye, #{val}”
}
method missing function __call($name,$args) {
$argc = count($args);
echo ”no def: $name “ . “arity:$argc\n”;
}
# in package:
our $AUTOLOAD; sub AUTOLOAD { my$self = shift;
my $argc = scalar(@_); print ”no def:$AUTOLOAD”
. ” arity: $argc\n”; } def __getattr__(self, name): s = ’no def: ‘+name+’ arity: %d’ return lambda *a: print(s % len(a)) def method_missing(name, *a) puts ”no def: #{name}” + ” arity: #{a.size}” end inheritance class Counter extends Int { private static$instances = 0;
function __construct($int=0) { Counter::$instances += 1;
parent::__construct($int); } function incr() {$this->value++;
}
static function getInstances() {
return $instances; } } package Counter; use Moose; extends ’Int’; my$instances = 0;
sub BUILD {
$instances += 1; } sub incr { my$self = shift;
my $v =$self->value;
$self->value($v + 1);
}
sub instances {
$instances; } no Moose; class Counter(Int): instances = 0 def __init__(self, v=0): Counter.instances += 1 Int.__init__(self, v) def incr(self): self.value += 1 class Counter < Int @@instances = 0 def initialize @@instances += 1 super end def incr self.value += 1 end def self.instances @@instances end end invoke class method Counter::getInstances() Counter->instances(); Counter.instances Counter.instances __________________________________________ __________________________________________ __________________________________________ __________________________________________ # General Footnotes ## versions used The versions used for testing code in the reference sheet. ## implicit prologue Code which examples in the sheet assume to have already been executed. perl: We adopt the convention that if an example uses a variable without declaring it, it should be taken to have been previously declared with my. python: To keep the examples short we assume that osre, and sys are always imported. ## show version How to get the version. php: The function phpversion() will return the version number as a string. perl: Also available in the predefined variable$], or in a different format in $^V and$PERL_VERSION.

python:

The following function will return the version number as a string:

import platform platform.python_version()

ruby:

Also available in the global constant VERSION (Ruby 1.8) or RUBY_VERSION (Ruby 1.9).

## interpreter

The customary name of the interpreter and how to invoke it.

php:

php -f will only execute portions of the source file within a <?php php code ?> tag as php code. Portions of the source file outside of such tags is not treated as executable code and is echoed to standard out.

If short tags are enabled, then php code can also be placed inside <? php code ?> and <?= php code ?> tags. <?= php code ?> is identical to <?php echo php code ?>.

## repl

The customary name of the repl.

php:

php -a is not a REPL. It collects input until EOF is encountered and then it executes it.

Text inside <? code ?> and <?= code ?> is executed as PHP code.

Text outside of PHP markup tags is echoed.

perl:

The Perl REPL perl -de 0 does not save or display the result of an expression. perl -d is the Perl debugger and perl -e runs code provided on the command line.

perl -de 0 does not by default have readline, but it can be added:

$cpan -i Term::Readline::Perl <cpan output omitted>$ perl -de 0 DB<1> use Term::Readline::Perl; DB<2> print 1 + 1; 2

python:

The python repl saves the result of the last statement in _.

ruby:

irb saves the result of the last statement in _.

## statement separator

How the parser determines the end of a statement.

php:

Inside braces statements must be terminated by a semicolon. The following causes a parse error:

<? if (true) { echo "true" } ?>

The last statement inside <?= ?> or <? ?> tags does not need to be semicolon terminated, however. The following code is legal:

<?= $a = 1 ?> <? echo$a ?>

perl:

In a script statements are separated by semicolons and never by newlines. However, when using ‘perl -de 0′, a newline terminates the statement.

python:

Newline does not terminate a statement when:

• inside parens
• inside list [] or dictionary {} literals

Python single quote ” and double quote “” strings cannot contain newlines except as the two character escaped form \n. Putting a newline in these strings results in a syntax error. There is however a multi-line string literal which starts and ends with three single quotes ”’ or three double quotes: “”".

A newline that would normally terminate a statement can be escaped with a backslash.

ruby:

Newline does not terminate a statement when:

• inside single quotes ”, double quotes “”, backticks “, or parens ()
• after an operator such as + or , that expects another argument

Ruby permits newlines in array [] or hash literals, but only after a comma , or associator =>. Putting a newline before the comma or associator results in a syntax error.

A newline that would normally terminate a statement can be escaped with a backslash.

## block delimiters

How blocks are delimited.

perl:

Curly brackets {} delimit blocks. They are also used for:

• hash literal syntax which returns a reference to the hash: $rh = { ‘true’ => 1, ‘false’ => 0 } • hash value lookup:$h{‘true’}, $rh->{‘true’} • variable name delimiter:$s = “hello”; print “${s}goodbye”; python: Python blocks begin with a line that ends in a colon. The block ends with the first line that is not indented further than the initial line. Python raises an IndentationError if the statements in the block that are not in a nested block are not all indented the same. Using tabs in Python source code is unrecommended and many editors replace them automatically with spaces. If the Python interpreter encouters a tab, it is treated as 8 spaces. The python repl switches from a >>> prompt to a … prompt inside a block. A blank line terminates the block. ruby: Curly brackets {} delimit blocks. A matched curly bracket pair can be replaced by the do and end keywords. By convention curly brackets are used for one line blocks. The end keyword also terminates blocks started by defclass, or module. Curly brackets are also used for hash literals, and the #{ } notation is used to interpolate expressions into strings. ## assignment How to assign a value to a variable. perl: Assignment operators have right precedence and evaluate to the right argument, so assignments can be chained: $a = $b = 3; python: If the variable on the left has not previously been defined in the current scope, then it is created. This may hide a variable in a containing scope. Assignment does not return a value and cannot be used in an expression. Thus, assignment cannot be used in a conditional test, removing the possibility of using assignment (=) in place of an equality test (==). Assignments can nevertheless be chained to assign a value to multiple variables: a = b = 3 ruby: Assignment operators have right precedence and evaluate to the right argument, so they can be chained. If the variable on the left does not exist, then it is created. ## parallel assignment How to assign values to variables in parallel. python: The r-value can be a list or tuple: nums = [1,2,3] a,b,c = nums more_nums = (6,7,8) d,e,f = more_nums Nested sequences of expression can be assigned to a nested sequences of l-values, provided the nesting matches. This assignment will set a to 1, b to 2, and c to 3: (a,[b,c]) = [1,(2,3)] This assignment raises a TypeError: ruby: The r-value can be an array: nums = [1,2,3] a,b,c = nums ## swap How to swap the values held by two variables. ## compound assignment Compound assignment operators mutate a variable, setting it to the value of an operation which takes the value of the variable as an argument. First row: arithmetic operator assignment: addition, subtraction, multiplication, (float) division, integer division, modulus, and exponentiation. Second row: string concatenation assignment and string replication assignment Third row: logical operator assignment: and, or, xor Fourth row: bit operator assignment: left shift, right shift, and, or, xor. python: Python compound assignment operators do not return a value and hence cannot be used in expressions. ## increment and decrement php: The increment and decrement operators also work on strings. There are postfix versions of these operators which evaluate to the value before mutation: $x = 1; $x++;$x--;

perl:

The increment and decrement operators also work on strings. There are postfix versions of these operators which evaluate to the value before mutation:

$x = 1;$x++; $x--; ruby: The Integer class defines succpred, and next, which is a synonym for succ. The String class defines succsucc!next, and next!succ! and next! mutate the string. ## local variable declarations How to declare variables which are local to the scope defining region which immediately contain them. php: Variables do not need to be declared and there is no syntax for declaring a local variable. If a variable with no previous reference is accessed, its value is NULL. perl: Variables don’t need to be declared unless use strict is in effect. If not initialized, scalars are set to undef, arrays are set to an empty array, and hashes are set to an empty hash. Perl can also declare variables with local. These replace the value of a global variable with the same name, if any, for the duration of the enclosing scope, after which the old value is restored. local declarations became obsolete with the introduction of the my declaration introduced in Perl 5. python: A variable is created by assignment if one does not already exist. If the variable is inside a function or method, then its scope is the body of the function or method. Otherwise it is a global. ruby: Variables are created by assignment. If the variable does not have a dollar sign ($) or ampersand (@) as its first character then its scope is scope defining region which most immediately contains it.

A lower case name can refer to a local variable or method. If both are defined, the local variable takes precedence. To invoke the method make the receiver explicit: e.g. self.name. However, outside of class and modules local variables hide functions because functions are private methods in the class Object. Assignment to name will create a local variable if one with that name does not exist, even if there is a method name.

## regions which define local scope

A list of regions which define a scope for the local variables they contain.

Local variables defined inside the region are only in scope while code within the region is executing. If the language does not have closures, then code outside the region has no access to local variables defined inside the region. If the language does have closures, then code inside the region can make local variables accessible to code outside the region by returning a reference.

A region which is top level hides local variables in the scope which contains it from the code it contains. A region can also be top level if the syntax requirements of the language prohibit it from being placed inside another scope defining region.

A region is nestable if it can be placed inside another cope defining region, and if code in the inner region can access local variables in the outer region.

php:

Only function bodies and method bodies define scope. Function definitions can be nested, but when this is done lexical variables in the outer function are not visible to code in the body of the inner function.

Braces can be used to set off blocks of codes in a manner similar to the anonymous blocks of Perl. However, these braces do not define a scope. Local variables created inside the braces will be visible to subsequent code outside of the braces.

Local variables cannot be created in class bodies.

perl:

A local variable can be defined outside of any function definition or anonymous block, in which case the scope of the variable is the file containing the source code. In this way Perl resembles Ruby and contrasts with PHP and Python. In PHP and Python, any variable defined outside a function definition is global.

In Perl, when a region which defines a scope is nested inside another, then the inner region has read and write access to local variables defined in the outer region.

Note that the blocks associated with the keywords ifunlesswhileuntilfor, and foreach are anonymous blocks, and thus any my declarations in them create variables local to the block.

python:

Only functions and methods define scope. Function definitions can be nested. When this is done, inner scopes have read access to variables defined in outer scopes. Attempting to write (i.e. assign) to a variable defined in an outer scope will instead result in a variable getting created in the inner scope. Python trivia question: what would happen if the following code were executed?

def foo(): v = 1 def bar(): print(v) v = 2 print(v) bar() foo()

ruby:

Note that though the keywords ifunlesscasewhile, and until each define a block which is terminated by an end keyword, none of these blocks have their own scope.

Anonymous functions can be created with the lambda keyword. Ruby anonymous blocks can be provided after a function invocation and are bounded by curly brackets { } or the do and end keywords. Both anonymous functions and anonymous blocks can have parameters which are specified at the start of the block within pipes. Here are some examples:

id = lambda { |x| x } [3,1,2,4].sort { |a,b| a <=> b } 10.times do |i| print "#{i}..." end

In Ruby 1.8, the scope of the parameter of an anonymous block or function or block is local to the block or function body if the name is not already bound to a variable in the containing scope. However, if it is, then the variable in the containing scope will be used. This behavior was changed in Ruby 1.9 so that parameters are always local to function body or block. Here is an example of code which behaves differently under Ruby 1.8 and Ruby 1.9:

x = 3 id = lambda { |x| x } id.call(7) puts x # 1.8 prints 7; 1.9 prints 3

Ruby 1.9 also adds the ability mark variables as local, even when they are already defined in the containing scope. All such variables are listed inside the parameter pipes, separated from the parameters by a semicolon:

x = 3 noop = lambda { |; x| x = 15 } # bad syntax under 1.8 noop.call # x is still 3

## global variable

How to declare and access a variable with global scope.

php:

A variable is global if it is used at the top level (i.e. outside any function definition) or if it is declared inside a function with the global keyword. A function must use theglobal keyword to access the global variable.

perl:

Undeclared variables, which are permitted unless use strict is in effect, are global. If use strict is in effect, a global can be declared at the top level of a package (i.e. outside any blocks or functions) with the our keyword. A variable declared with my inside a function will hide a global with the same name, if there is one.

python:

A variable is is global if it is defined at the top level of a file (i.e. outside any function definition). Although the variable is global, it must be imported individuality or be prefixed with the module name prefix to be accessed from another file. To be accessed from inside a function or method it must be declared with the global keyword.

ruby:

## float division

How to perform floating point division, even if the operands might be integers.

## arithmetic functions

Some arithmetic functions. Trigonometric functions are in radians unless otherwise noted. Logarithms are natural unless otherwise noted.

python:

Python also has math.log10. To compute the log of x for base b, use:

math.log(x)/math.log(b)

ruby:

Ruby also has Math.log2Math.log10. To compute the log of x for base b, use

Math.log(x)/Math.log(b)

## arithmetic truncation

How to truncate a float to the nearest integer towards zero; how to round a float to the nearest integer; how to find the nearest integer above a float; how to find the nearest integer below a float; how to take the absolute value.

perl:

The CPAN module Number::Format provides a round function. The 2nd argument specifies the number of digits to keep to the right of the radix. The default is 2.

use Number::Format 'round'; round(3.14, 0);

## min and max

How to get the min and max.

## division by zero

What happens when division by zero is performed.

## integer overflow

What happens when the largest representable integer is exceeded.

## float overflow

What happens when the largest representable float is exceeded.

## sqrt -2

The result of taking the square root of negative two.

## rational numbers

How to create rational numbers and get the numerator and denominator.

ruby:

Require the library mathn and integer division will yield rationals instead of truncated integers.

## complex numbers

python:

Most of the functions in math have analogues in cmath which will work correctly on complex numbers.

## random integer, uniform float, normal float

How to generate a random integer between 0 and 99, include, float between zero and one in a uniform distribution, or a float in a normal distribution with mean zero and standard deviation one.

## set random seed, get and restore seed

How to set the random seed; how to get the current random seed and later restore it.

All the languages in the sheet set the seed automatically to a value that is difficult to predict. The Ruby 1.9 MRI interpreter uses the current time and process ID, for example. As a result there is usually no need to set the seed.

Setting the seed to a hardcoded value yields a random but repeatable sequence of numbers. This can be used to ensure that unit tests which cover code using random numbers doesn’t intermittently fail.

The seed is global state. If multiple functions are generating random numbers then saving and restoring the seed may be necessary to produce a repeatable sequence.

## bit operators

The bit operators for left shift, right shift, and, inclusive or, exclusive or, and negation.

## binary, octal, and hex literals

Binary, octal, and hex integer literals

## base conversion

How to convert integers to strings of digits of a given base. How to convert such strings into integers.

perl

Perl has the functions oct and hex which convert strings encoded in octal and hex and return the corresponding integer. The
oct function will handle binary or hex encoded strings if they have “0b” or “0x” prefixes.

oct("60") oct("060") oct("0b101010") oct("0x2a") hex("2a") hex("0x2a")

python

Python has the functions binoct, and hex which take an integer and return a string encoding the integer in base 2, 8, and 16.

bin(42) oct(42) hex(42)

# String Footnotes

## string literal

The syntax for string literals.

## newline in literal

Whether newlines are permitted in string literals.

python:

Newlines are not permitted in single quote and double quote string literals. A string can continue onto the following line if the last character on the line is a backslash. In this case, neither the backslash nor the newline are taken to be part of the string.

Triple quote literals, which are string literals terminated by three single quotes or three double quotes, can contain newlines:

'''This is two lines''' """This is also two lines"""

## backslash escapes

Backslash escape sequences for inserting special characters into string literals.

python:

When string literals have an r or R prefix there are no backslash escape sequences and any backslashes thus appear in the created string. The delimiter can be inserted into a string if it is preceded by a backslash, but the backslash is also inserted. It is thus not possible to create a string with an r or R prefix that ends in a backslash. The r and R prefixes can be used with single or double quotes:

r'C:\Documents and Settings\Admin' r"C:\Windows\System32"

## variable interpolation

How to interpolate variables into strings.

python:

Python lacks interpolating quotes. Except for the fact that they can contain single quotes, double quotes are identical to single quotes.

## custom delimiters

How to specify custom delimiters for single and double quoted strings. These can be used to avoid backslash escaping. If the left delimiter is (, [, or { the right delimiter must be ), ], or }, respectively.

## sprintf

How to create a string using a printf style format.

python:

Escape curly braces by doubling:

'to insert parameter {0} into a format, use {{{0}}}'.format(3)

## here document

Here documents are strings terminated by a custom identifier. They perform variable substitution and honor the same backslash escapes as double quoted strings.

perl:

Put the custom identifer in single quotes to prevent variable interpolation and backslash escape interpretation:

s = <<'EOF'; Perl code uses variables with dollar signs, e.g. $var EOF python: Python lacks variable interpolation in strings. Triple quotes honor the same backslash escape sequences as regular quotes, so triple quotes can otherwise be used like here documents: s = '''here document there computer ''' ruby: Put the customer identifier in single quotes to prevent variable interpolation and backslash escape interpretation: s = <<'EOF' Ruby code uses #{var} type syntax to interpolate variables into strings. EOF ## concatenate The string concatenation operator. ## replicate The string replication operator. ## split, split in two, split into characters How to split a string containing a separator into an array of substrings; how to split a string in two; how to split a string into an array of single character strings ## join How to concatenate the elements of an array into a string with a separator. ## case manipulation How to put a string into all caps or all lower case letters. How to capitalize the first letter of a string. ## strip How to remove whitespace from the ends of a string. ## pad on right, on left How to pad the edge of a string with spaces so that it is a prescribed length. ## length How to get the length in characters of a string. ## index of substring How to find the index of the leftmost occurrence of a substring in a string. ## extract substring How to extract a substring from a string by index. ## extract character How to extract a character from a string by its index. ## chr and ord Converting characters to ASCII codes and back. The languages in this reference sheet do not have character literals, so characters are represented by strings of length one. ## character translation How to apply a character mapping to a string. # Regular Expressions Regular expressions or regexes are a way of specifying sets of strings. If a string belongs to the set, the string and regex “match”. Regexes can also be used to parse strings. The modern notation for regexes was introduced by Unix command line tools in the 1970s. POSIX standardized the notation into two types: extended regexes and the more archaic basic regexes. Perl regexes are extended regexes augmented by new character class abbreviations and a few other features introduced by the Perl interpreter in the 1990s. All the languages in this sheet use Perl regexes. Any string that doesn’t contain regex metacharacters is a regex which matches itself. The regex metacharacters are: [ ] . | ( ) * + ? { } ^$ \

character classes: [ ] .

A character class is a set of characters in brackets: [ ]. When used in a regex it matches any character it contains.

Character classes have their own set of metacharacters: ^ – \ ]

The ^ is only special when it the first character in the character class. Such a character class matches its complement; that is, any character not inside the brackets. When not the first character the ^ refers to itself.

The hypen is used to specify character ranges: e.g. 0-9 or A-Z. When the hyphen is first or last inside the brackets it matches itself.

The backslash can be used to escape the above characters or the terminal character class delimiter: ]. It can be used in character class abbreviations or string backslash escapes.

The period . is a character class abbreviation which matches any character except for newline. In all languages the period can be made to match all characters. In PHP and Perl use the m modifer. In Python use the re.M flag. In Ruby use the s modifer.

character class abbreviations:

abbrev name character class
\d digit [0-9]
\D nondigit [^0-9]
\h PHP, Perl: horizontal whitespace character
Ruby: hex digit
PHP, Perl: [ \t]
Ruby: [0-9a-fA-F]
\H PHP, Perl: not a horizontal whitespace character
Ruby: not a hex digit
PHP, Perl: [^ \t]
Ruby: [^0-9a-fA-F]
\s whitespace character [ \t\r\n\f]
\S non whitespace character [^ \t\r\n\f]
\v vertical whitespace character [\r\n\f]
\V not a vertical whitespace character [^\r\n\f]
\w word character [A-Za-z0-9_]
\W non word character [^A-Za-z0-9_]

alternation and grouping: | ( )

The vertical pipe | is used for alternation and parens () for grouping.

A vertical pipe takes as its arguments everything up to the next vertical pipe, enclosing paren, or end of string.

Parentheses control the scope of alternation and the quantifiers described below. The are also used for capturing groups, which are the substrings which matched parenthesized parts of the regular expression. Each language numbers the groups and provides a mechanism for extracting when a match is made. A parenthesized subexpression can be removed from the groups with this syntax: (?:expr)

quantifiers: * + ? { }

As an argument quantifiers take the preceding regular character, character class, or group. The argument can itself be quantified, so that ^a{4}*$matches strings with the letter a in multiples of 4. quantifier # of occurrences of argument matched * zero or more, greedy + one or more, greedy ? zero or one, greedy {m,n} m to n, greedy {n} exactly n {m,} m or more, greedy {,n} zero to n, greedy *? zero or more, lazy +? one or more, lazy {m,n}? m to n, lazy {m,}? m or more, lazy {,n}? zero to n, lazy When there is a choice, greedy quantifiers will match the maximum possible number of occurrences of the argument. Lazy quantifiers match the minium possible number. anchors: ^$

anchor matches
^ beginning of a string. In Ruby or when m modifier is used also matches right side of a newline
$end of a string. In Ruby or when m modifier is used also matches left side of a newline \A beginning of the string \b word boundary. In between a \w and a \W character or in between a \w character and the edge of the string \B not a word boundary. In between two \w characters or two \W characters \z end of the string \Z end of the string unless it is a newline, in which case it matches the left side of the terminal newline escaping: \ To match a metacharacter, put a backslash in front of it. To match a backslash use two backslashes. php: PHP 5.3 still supports the EREG engine, though the functions which use it are deprecated. These include the split function and functions which start with ereg. The preferred functions are preg_split and the other functions with a preg prefix. ## literal, custom delimited literal The literal for a regular expression; the literal for a regular expression with a custom delimiter. php: PHP regex literals are strings. The first character is the delimiter and it must also be the last character. If the start delimiter is (, {, or [ the end delimiter must be ), }, or ], respectively. Here are the signatures from the PHP manual for the preg functions used in this sheet: array preg_split ( string$pattern , string $subject [, int$limit = -1 [, int $flags = 0 ]] ) int preg_match ( string$pattern , string $subject [, array &$matches [, int $flags = 0 [, int$offset = 0 ]]] ) mixed preg_replace ( mixed $pattern , mixed$replacement , mixed $subject [, int$limit = -1 [, int &$count ]] ) int preg_match_all ( string$pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int$offset = 0 ]]] )

python:

Python does not have a regex literal, but the re.compile function can be used to create regex objects.

Compiling regexes can always be avoided:

re.compile('\d{4}').search('1999') re.search('\d{4}', '1999') re.compile('foo').sub('bar', 'foo bar') re.sub('foo', 'bar', 'foo bar') re.compile('\w+').findall('do re me') re.findall('\w+', 'do re me')

## character class abbreviations and anchors

The supported character class abbreviations and anchors.

Note that \h refers to horizontal whitespace (i.e. a space or tab) in PHP and Perl and a hex digit in Ruby. Similarly \H refers to something that isn’t horizontal whitespace in PHP and Perl and isn’t a hex digit in Ruby.

## match test

How to test whether a string matches a regular expression.

python:

The re.match function is like the re.search function, except that it is only true if the regular expression matches the entire string.

ruby:

match is a method of both Regexp and String so can match with both

/1999/.match("1999")

and

"1999".match(/1999/)

When variables are involved it is safer to invoke the Regexp method because string variables are more likely to contain nil.

## case insensitive match test

How to perform a case insensitive match test.

## modifiers

Modifiers that can be used to adjust the behavior of a regular expression.

The lists are not comprehensive. For all languages except Ruby there are additional modifiers.

modifier behavior
e PHP: when used with preg_replace, the replacement string, after backreferences are substituted, is eval’ed as PHP code and the result is used as the replacement.
i, re.I all: ignores case. Upper case letters match lower case letters and vice versa.
m, re.M PHP, Perl, Python: makes the ^ and $match the right and left edge of newlines in addition to the beginning and end of the string. Ruby: makes the period . match newline characters. o Ruby: performs variable interpolation #{ } only once per execution of the program. p Perl: sets${^MATCH} ${^PREMATCH} and${^POSTMATCH}
s, re.S PHP, Perl, Python: makes the period . match newline characters.
x, re.X all: ignores whitespace in the regex which permits it to be used for formatting.

Python modifiers are bit flags. To use more than one flag at the same time, join them with bit or: |

## substitution

How to replace all occurrences of a matching pattern in a string with the provided substitution string.

php:

The number of occurrences replaced can be controlled with a 4th argument to preg_replace:

$s = "foo bar bar"; preg_replace('/bar/', "baz",$s, 1);

If no 4th argument is provided, all occurrences are replaced.

perl:

The =~ operator performs the substitution in place on the string and returns the number of substitutions performed.

The g modifiers specifies that all occurrences should be replaced. If omitted, only the first occurrence is replaced.

python:

The 3rd argument to sub controls the number of occurrences which are replaced.

s = 'foo bar bar' re.compile('bar').sub('baz', s, 1)

If there is no 3rd argument, all occurrences are replaced.

ruby:

The gsub operator returns a copy of the string with the substitution made, if any. The gsub! performs the substitution on the original string and returns the modified string.

The sub and sub! operators only replace the first occurrence of the match pattern.

## match, prematch, postmatch

How to get the substring that matched the regular expression, as well as the part of the string before and after the matching substring.

perl:

The special variables $&$, and $' also contain the match, prematch, and postmatch. ruby: The special variables$&$, and$' also contain the match, prematch, and postmatch.

## group capture

How to get the substrings which matched the parenthesized parts of a regular expression.

ruby:

Ruby has syntax for extracting a group from a match in a single expression. The following evaluates to “1999″:

"1999-07-08"[/(\d{4})-(\d{2})-(\d{2})/, 1]

## scan

How to return all non-overlapping substrings which match a regular expression as an array.

## backreference in match and substitution

How to use backreferences in a regex; how to use backreferences in the replacement string of substitution.

## recursive regex

Examples of recursive regexes.

The examples match substrings containing balanced parens.

# Date and Time Footnotes

In ISO 8601 terminology, a date specifies a day in the Gregorian calendar and a time does not contain date information; it merely specifies a time of day. A data type which combines both date and time information is probably more useful than one which contains just date information or just time information; it is unfortunate that ISO 8601 doesn’t provide a name for this entity. The word timestamp often gets used to denote a combined date and time. PHP and Python use the compound noundatetime for combined date and time values.

An useful property of ISO 8601 dates, times, and date/time combinations is that they are correctly ordered by a lexical sort on their string representations. This is because they are big-endian (the year is the leftmost element) and they used fixed-length fields for each term in the string representation.

The C standard library provides two methods for representing dates. The first is the UNIX epoch, which is the seconds since January 1, 1970 in UTC. If such a time were stored in a 32-bit signed integer, the rollover would happen on January 18, 2038.

The other method of representing dates is the tm struct, a definition of which can be found on Unix systems in /usr/include/time.h:

struct tm { int tm_sec; /* seconds after the minute [0-60] */ int tm_min; /* minutes after the hour [0-59] */ int tm_hour; /* hours since midnight [0-23] */ int tm_mday; /* day of the month [1-31] */ int tm_mon; /* months since January [0-11] */ int tm_year; /* years since 1900 */ int tm_wday; /* days since Sunday [0-6] */ int tm_yday; /* days since January 1 [0-365] */ int tm_isdst; /* Daylight Savings Time flag */ long tm_gmtoff; /* offset from CUT in seconds */ char *tm_zone; /* timezone abbreviation */ };

Perl and Python both use and expose the tm struct of the standard library. In the case of Perl, the first nine values of the struct (up to the member tm_isdst) are put into an array. Python, meanwhile, has a module called time which is a thin wrapper to the standard library functions which operate on this struct. Here is how get a tmstruct in Python:

import time utc = time.gmtime(time.time()) t = time.localtime(time.time())

The tm struct is a low level entity, and interacting with it directly should be avoided. In the case of Python it is usually sufficient to use the datetime module instead. For Perl, one can use the Time::Piece module to wrap the tm struct in an object.

## date/time type

The data type used to hold a combined date and time.

perl

Built in Perl functions work with either (1) scalars containing the Unix epoch as an integer or (2) arrays containing the first nine values of the standard C library tm struct. When use Time::Piece is in effect functions which work with tm arrays are replaced with variant that work with the Time::Piece wrapper.

The modules Time::Local and Date::Parse can create scalars containing the Unix epoch.

CPAN provides the DateTime module which provides objects with functionality comparable to the DateTime objects of PHP and Python.

## current date/time

How to get the combined date and time for the present moment in both local time and UTC.

## to unix epoch, from unix epoch

How to convert the native date/time type to the Unix epoch which is the number of seconds since the start of January 1, 1970 UTC.

## current unix epoch

How to get the current time as a Unix epoch timestamp.

## strftime

How to format a date/time as a string using the format notation of the strftime function from the standard C library. This same format notation is used by the Unix datecommand.

php:

PHP supports strftime but it also has its own time formatting system used by dateDateTime::format, and DateTime::createFromFormat. The letters used in the PHP time formatting system are described here.

## default format example

Examples of how a date/time object appears when treated as a string such as when it is printed to standard out.

The formats are in all likelihood locale dependent. The provided examples come from a machine running Mac OS X in the Pacific time zone of the USA.

php:

It is a fatal error to treat a DateTime object as a string.

## strptime

How to parse a date/time using the format notation of the strptime function from the standard C library.

## parse date w/o format

How to parse a date without providing a format string.

## result date subtraction

The data type that results when subtraction is performed on two combined date and time values.

How to add a time duration to a date/time.

A time duration can easily be added to a date/time value when the value is a Unix epoch value.

ISO 8601 distinguishes between a time interval, which is defined by two date/time endpoints, and a duration, which is the length of a time interval and can be defined by a unit of time such as ’10 minutes’. A time interval can also be defined by date and time representing the start of the interval and a duration.

ISO 8601 defines notation for durations. This notation starts with a ‘P’ and uses a ‘T’ to separate the day and larger units from the hour and smaller units. Observing the location relative to the ‘T’ is important for interpreting the letter ‘M’, which is used for both months and minutes.

## local timezone

Do date/time values include timezone information. When a date/time value for the local time is created, how the local timezone is determined.

A date/time value can represent a local time but not have any timezone information associated with it.

On Unix systems processes determine the local timezone by inspecting the file /etc/localtime.

php:

The default timezone can also be set in the php.ini file.

date.timezone = "America/Los_Angeles"

Here is the list of timezones supported by PHP.

## timezone name, offset from UTC, is daylight savings?

How to get time zone information: the name of the timezone, the offset in hours from UTC, and whether the timezone is currently in daylight savings.

Timezones are often identified by three or four letter abbreviations. As can be seen from the list, many of the abbreviations do not uniquely identify a timezone. Furthermore many of the timezones have been altered in the past. The Olson database (aka Tz database) decomposes the world into zones in which the local clocks have all been set to the same time since 1970 and it gives these zones unique names.

perl:

It is not necessary to create a DateTime object to get the local timezone offset:

## quote words

The quote words operator, which is a literal for arrays of strings where each string contains a single word.

## size

How to get the number of elements in an array.

## empty test

How to test whether an array is empty.

## lookup

How to access a value in an array by index.

perl:

A negative index refers to the length – index element.

python:

A negative index refers to the length – index element.

>>> a = [1,2,3] >>> a[-1] 3

ruby:

A negative index refers to to the length – index element.

## update

How to update the value at an index.

## out-of-bounds behavior

What happens when the value at an out-of-bounds index is refererenced.

perl:

## slice by endpoints, by length

How to slice a subarray from an array by specifying a start index and an end index; how to slice a subarray from an array by specifying an offset index and a length index.

perl:

Perl arrays can take an array of indices as the index value. The range of values selected can be discontinuous and the order of the values can be manipulated:

@nums = (1,2,3,4,5,6); @nums[(1,3,2,4)];

python:

Slices can leave the first or last index unspecified, in which case the first or last index of the sequence is used:

>>> a=[1,2,3,4,5] >>> a[:3] [1, 2, 3]

Python has notation for taking every nth element:

>>> a=[1,2,3,4,5] >>> a[::2] [1, 3, 5]

The third argument in the colon-delimited slice argument can be negative, which reverses the order of the result:

>>> a = [1,2,3,4] >>> a[::-1] [4, 3, 2, 1]

## slice to end

How to slice to the end of an array.

The examples take all but the first element of the array.

## manipulate back

How to add and remove elements from the back or high index end of an array.

These operations can be used to use the array as a stack.

## manipulate front

How to add and remove elements from the front or low index end of an array.

These operations can be used to use the array as a stack. They can be used with the operations that manipulate the back of the array to use the array as a queue.

## concatenate

How to create an array by concatenating two arrays; how to modify an array by concatenating another array to the end of it.

## address copy, shallow copy, deep copy

How to make an address copy, a shallow copy, and a deep copy of an array.

After an address copy is made, modifications to the copy also modify the original array.

After a shallow copy is made, the addition, removal, or replacement of elements in the copy does not modify of the original array. However, if elements in the copy are modified, those elements are also modified in the original array.

A deep copy is a recursive copy. The original array is copied and a deep copy is performed on all elements of the array. No change to the contents of the copy will modify the contents of the original array.

perl:

## zip

How to interleave arrays. In the case of two arrays the result is an array of pairs or an associative list.

perl:

zip expects arrays as arguments, which makes it difficult to define the arrays to be zipped on the same line as the invocation. It can be done like this:

@a = zip @{[1,2,3]}, @{['a','b','c']};

# Dictionary Footnotes

## literal

perl:

Curly brackets create a hash and return a reference to it:

$h = { 'hello' => 5, 'goodbye' => 7 } ## size How to get the number of dictionary keys in a dictionary. ## lookup How to lookup a dictionary value using a dictionary key. perl: Use the ampersand prefix @ to slice a Perl hash. The index is a list of keys. %nums = ('b'=>1, 't'=>2, 'a'=>3); @nums{('b','t')} ## out-of-bounds behavior What happens when a lookup is performed on a key that is not in a dictionary. ## is key present How to check for the presence of a key in a dictionary without raising an exception. Distinguishes from the case where the key is present but mapped to null or a value which evaluates to false. ## delete entry How to remove a key/value pair from a dictionary. ## from array of pairs, from even length array How to create a dictionary from an array of pairs; how to create a dictionary from an even length array. ## merge How to merge the values of two dictionaries. In the examples, if the dictionaries d1 and d2 share keys then the values from d2 will be used in the merged dictionary. ## invert How to turn a dictionary into its inverse. If a key ‘foo’ is mapped to value ‘bar’ by a dictionary, then its inverse will map the key ‘bar’ to the value ‘foo’. However, if multiple keys are mapped to the same value in the original dictionary, then some of the keys will be discarded in the inverse. ## iteration How to iterate through the key/value pairs in a dictionary. python: If d contains a dictionary then d.items() returns the dictionary as an associative list and d.iteritems() returns an iterator on the dictionary as an associative list. ## keys and values as arrays How to convert the keys of a dictionary to an array; how to convert the values of a dictionary to an array. ## set default value How to create a dictionary with a default value for missing keys. perl: How to use a tied hash. If the CPAN module Tie::ExtraHash is installed there is a shorter way. # Function Footnotes Python has both functions and methods. Ruby only has methods: functions defined at the top level are in fact methods on a special main object. Perl subroutines can be invoked with a function syntax or a method syntax. ## function declaration How to define a function. ## function invocation How to invoke a function. python: When invoking methods and functions, parens are mandatory, even for functions which take no arguments. Omitting the parens returns the function or method as an object. Whitespace can occur between the function name and the following left paren. Starting with 3.0, print is treated as a function instead of a keyword. Thus parens are mandatory around the print argument. ruby: Ruby parens are optional. Leaving out the parens results in ambiguity when function invocations are nested. The interpreter resolves the ambiguity by assigning as many arguments as possible to the innermost function invocation, regardless of its actual arity. As of Ruby 1.9, it is mandatory that the left paren not be separated from the method name by whitespace. ## missing argument behavior How incorrect number of arguments upon invocation are handled. perl: Perl collects all arguments into the @_ array, and subroutines normally don’t declare the number of arguments they expect. However, this can be done with prototypes. Prototypes also provide a method for taking an array from the caller and giving a reference to the array to the callee. python: TypeError is raised if the number of arguments is incorrect. ruby: ArgumentError is raised if the number of arguments is incorrect. ## default value How to declare a default value for an argument. ## arbitrary number of arguments How to write a function which accepts a variable number of argument. php: It is also possible to use func_num_args and func_get_arg to access the arguments: for ($i = 1; $i < func_num_args();$i++) { echo func_get_arg($i); } ## named parameter definition and invocation How to write a function which uses named parameters and how to invoke it. python: In a function definition, the splat operator * collects the remaining arguments into a list. In a function invocation, the splat can be used to expand an array into separate arguments. In a function definition the double splat operator ** collects named parameters into a dictionary. In a function invocation, the double splat expands a dictionary into named parameters. ruby: ## pass number or string by reference How to pass numbers or strings by reference. The three common methods of parameter passing are pass by valuepass by reference, and pass by address. Pass by value is the default in most languages. When a parameter is passed by reference, the callee can changed the value in the variable that was provided as a parameter, and the caller will see the new value when the callee returns. When the parameter is passed by value the callee cannot do this. When a language has mutable data types it can be unclear whether the language is using pass by value or pass by reference. perl: Here is a potential for confusion: if a reference is used in Perl to pass data, that is pass by address, not pass by reference. A Perl reference is comparable to a pointer in C, albeit one that knows the data type of what it points to at runtime. C++ has both pointers and references and thus can pass data by address or by reference, though pass by value is the default. ## pass array or dictionary by reference How to pass an array or dictionary without making a copy of it. perl: Arrays and hashes are not passed by reference by default. If an array is provided as a argument, each element of the array will be assigned to a parameter. A change to the parameter will change the corresponding value in the original array, but the number of elements in the array cannot be increased. To write a function which changes the size of the array the array must be passed by reference using the backslash notation. When a hash is provided as a argument each key of the has will be assigned to a parameter and each value of the hash will be assigned to a parameter. In other words the number of parameters seen by the body of the function will be twice the size of the hash. Each value parameter will immediately follow its key parameter. ## return value How the return value of a function is determined. ## multiple return values How to return multiple values from a function. ## lambda declaration and invocation How to define and invoke a lambda function. python: Python lambdas cannot contain newlines or semicolons, and thus are limited to a single statement or expression. Unlike named functions, the value of the last statement or expression is returned, and a return is not necessary or permitted. Lambdas are closures and can refer to local variables in scope, even if they are returned from that scope. If a closure function is needed that contains more than one statement, use a nested function: def make_nest(x): b = 37 def nest(y): c = x*y c *= b return c return nest n = make_nest(12*2) print(n(23)) Python closures are read only. A nested function can be returned and hence be invoked outside of its containing function, but it is not visible by its name outside of its containing function. ## function with private state How to create a function with private state which persists between function invocations. ## closure How to create a first class function with access to the local variables of the local scope in which it was created. python: Python has limited closures: access to local variables in the containing scope is read only and the bodies of anonymous functions must consist of a single expression. ## generator How to create a function which can yield a value back to its caller and suspend execution. perl: CPAN provides a module called Coro which implements coroutines. Some notes on the distinction between coroutines and generators. python: Python generators can be used in for/in statements and list comprehensions. ruby: Ruby generators are called fibers. # Execution Control Footnotes ## if The if statement. php: PHP has the following alternate syntax for if statements: if ($n == 0): echo "no hits\n"; elseif ($n == 1): echo "one hit\n"; else: echo "$n hits\n"; endif;

perl:

When an if block is the last statement executed in a subroutine, the return value is the value of the branch that executed.

ruby:

If an if statement is the last statement executed in a function, the return value is the value of the branch that executed.

Ruby if statements are expressions. They can be used on the right hand side of assignments:

m = if n 1 else 0 end

## switch

The switch statement.

## while

php:

PHP provides a do-while loop. The body of such a loop is guaranteed to execute at least once.

$i = 0; do { echo$i; } while ($i > 0); perl: Perl provides untildo-while, and do-until loops. An until or a do-until loop can be replaced by a while or a do-while loop by negating the condition. ruby: Ruby provides a loop with no exit condition: def yes(expletive="y") loop do puts expletive end end Ruby also provides the until loop. Ruby loops can be used in expression contexts but they always evaluate to nil. ## c-style for How to write a C-style for loop. ## break, continue, redo break exits a for or while loop immediately. continue goes to the next iteration of the loop. redo goes back to the beginning of the current iteration. ## control structure keywords A list of control structure keywords. The loop control keywords from the previous line are excluded. The list summarizes the available control structures. It excludes the keywords for exception handling, loading libraries, and returning from functions. ## what do does How the do keyword is used. perl: The do keyword can convert an if statement to a conditional expression: my$m = do { if ($n) { 1 } else { 0 } }; ## statement modifiers Clauses added to the end of a statement to control execution. Perl and Ruby have conditional statement modifers. Ruby also has looping statement modifers. ruby: Ruby has the looping statement modifiers while and until: i = 0 i += 1 while i < 10 j = 10 j -= 1 until j < 0 ## raise exception How to raise exceptions. ruby: Ruby has a throw keyword in addition to raisethrow can have a symbol as an argument, and will not convert a string to a RuntimeError exception. ## catch exception How to catch exceptions. php: PHP code must specify a variable name for the caught exception. Exception is the top of the exception hierarchy and will catch all exceptions. Internal PHP functions usually do not throw exceptions. They can be converted to exceptions with this signal handler: function exception_error_handler($errno, $errstr,$errfile, $errline ) { throw new ErrorException($errstr, 0, $errno,$errfile, $errline); } set_error_handler("exception_error_handler"); ruby: rescue Exception clause will catch any exception. A rescue clause with no exception type specified will catch exceptions that are subclasses of StandardError. Exceptions outside StandardError are usually unrecoverable and hence not handled in code. In a rescue clause, the retry keyword will cause the begin clause to be re-executed. In addition to begin and rescue, ruby has catch: catch (:done) do loop do retval = work throw :done if retval < 10 end end ## global variable for last exception The global variable name for the last exception raised. ## define exception How to define a new variable class. ## catch exception by type How to catch exceptions of a specific type and assign the exception a name. php: PHP exceptions when caught must always be assigned a variable name. ## finally/ensure Clauses that are guaranteed to be executed even if an exception is thrown or caught. ## start thread ruby: Ruby 1.8 threads are green threads, and the interpreter is limited to a single operating system thread. ## wait on thread How to make a thread wait for another thread to finish. # File Footnotes ## print to standard output python: print appends a newline to the output. To suppress this behavior, put a trailing comma after the last argument. If given multiple arguments, print joins them with spaces. In Python 2 print parses as a keyword and parentheses are not required: print "Hello, World!" ruby: puts appends a newline to the output. print does not. ## read from standard input How to read from standard input. ## standard file handles The names for standard input, standard output, and standard error. ## open file ruby: When File.open is given a block, the file is closed when the block terminates. ## open file for writing How to open a file for writing. ## close file How to close a file. ## read line How to read up to the next newline in a file. ## iterate over file by line How to iterate over a file line by line. ## chomp Remove a newline, carriage return, or carriage return newline pair from the end of a line if there is one. php: chop removes all trailing whitespace. It is an alias for rtrim. perl: chomp modifies its argument, which thus must be a scalar, not a string literal. python: Python strings are immutable. rstrip returns a modified copy of the string. rstrip(‘\r\n’) is not identical to chomp because it removes all contiguous carriage returns and newlines at the end of the string. ruby: chomp! modifies the string in place. chomp returns a modified copy. ## read entire file into array or string How to read the contents of a file into memory. ## write to file How to write to a file handle. ## flush file handle How to flush a file handle that has been written to. ## file test, regular file test How to test whether a file exists; how to test whether a file is a regular file (i.e. not a directory, special device, or named pipe). ## copy file, remove file, rename file How to copy a file; how to remove a file; how to rename a file. ## set file permissions How to set the permissions on the file. For Perl, Python, and Ruby, the mode argument is in the same format as the one used with the Unix chmod command. It uses bitmasking to get the various permissions which is why it is normally an octal literal. The mode argument should not be provided as a string such as “0755″. Python and Ruby will raise an exception if a string is provided. Perl will convert “0755″ to 755 and not 0755 which is equal to 493 in decimal. ## temporary file How to create and use a temporary file. Temporary file libraries solve two problems: (1) finding a unused pathname, and (2) putting the file in a location where the system will eventually remove it should the application fail to clean up after itself. php: tmpfile() returns a file handle. There is no way to get the pathname and the file is removed when the file handle is closed. # Directory Footnotes ## build pathname How to construct a pathname without hard coding the system file separator. ## dirname and basename How to extract the directory portion of a pathname; how to extract the non-directory portion of a pathname. ## iterate over directory by file How to iterate through the files in a directory. php: The code in the example will stop if a filename which evaluates as FALSE is encountered. One such filename is “0″. A safer way to iterate through the directory is: if ($dir = opendir("/etc")) { while (FALSE !== ($file = readdir($dir))) { echo "$file\n"; } closedir($dir); }

python:

file() is the file handle constructor. file can be used as a local variable name but doing so hides the constructor. It can still be invoked by the synonym open(), however.

## make directory

How to create a directory.

If needed, the examples will create more than one directory.

No error will result if a directory at the pathname already exists. An exception will be raised if the pathname is occupied by a regular file, however.

## recursive copy

How to perform a recursive copy. If the source is a directory, then the directory and all its contents will be copied.

## remove empty directory

How to remove an empty directory. The operation will fail if the directory is not empty.

## remove directory and contents

How to remove a directory and all its contents.

## directory test

How to determine if a pathname is a directory.

# Processes and Environment Footnotes

## exit

python:

It is possible to register code to be executed upon exit:

import atexit atexit.register(print, "goodbye")

It is possible to terminate a script without executing registered exit code by calling os._exit.

ruby:

It is possible to register code to be executed upon exit:

at_exit { puts "goodbye" }

The script can be terminated without executing registered exit code by calling exit!.

## backticks

How to invoke an external command and read its output into a variable.

The use of backticks for this operation goes back to the Bourne shell (1977).

perl:

The qx operator can be used with any delimiter. If the opening delimiter is (, [, or {, the closing delimiter must be ), ], or }.

ruby:

%x can be used with any delimiter. If the opening delimiter is (, [, or {, the closing delimiter must be ), ], or }.

# Library and Module Footnotes

How terminology is used in this sheet:

• library: code in its own file that can be loaded by client code. For interpreted languages, loading a library usually means parsing the library to intermediate representation used by the interpreter VM. It is useless to load an library and not make its definitions available under names in the client code. Hence it is common for languages to import identifiers defined in the library automatically when the library is loaded.
• module: a set of names that can be imported a unit. Importing a identifier means adding it to a scope. Importing a module means adding all the identifers in the module to a scope.
• package: a library that can be installed by a package manager.

A few notes:

According to our terminology, Perl and Java packages are modules, not packages.

PHP and C++ namespaces are another of example of modules.

We prefer to reserve the term namespace for divisions of the set of names imposed by the parser. For example, the identifier foo in the Perl variables $foo and @foobelong to different namespaces. Another example of namespaces in this sense is the Lisp-1 vs. Lisp-2 distinction: Scheme is a Lisp-1 and has a single namespace, whereas Common Lisp is a Lisp-2 and has multiple namespaces. Some languages (e.g. Python, Java) impose a one-to-one mapping between libraries and modules. All the definitions for a module must be in a single file, and there are typically restrictions on how the file must be named and where it is located on the filesystem. Other languages allow the definitions for a module to be spread over multiple files or permit a file to contain multiple modules. Ruby and C++ are such languages. ## load library Execute the specified file. Normally this is used on a file which only contains declarations at the top level. php: include_once behaves like require_once except that it is not fatal if an error is encountered executing the library. If it is desirable to reload the library even if it might already have been loaded, use require or include. perl: The last expression in a perl library must evaluate to true. When loading a library with use, the suffix of the file must be .pm. The do directive will re-execute a library even if it has already been loaded. ## reload library How to reload a library. Altered definitions in the library will replace previous versions of the definition. ## library path How to augment the library path by calling a function or manipulating a global variable. ## library path environment variable How to augment the library path by setting an environment variable before invoking the interpreter. ## library path command line option How to augment the library path by providing a command line option when invoking the interpreter. ## main in library How to put code in a library which executes when the file is run as a top-level script and not when the file is loaded as a library. ## module declaration How to declare a section of code as belonging to a module. ## submodule declaration How to declare a section of code as belonging to a submodule. ## module separator The punctuation used to separate the labels in the full name of a submodule. ## import all definitions in module How to import all the definitions in a module. ## import definitions How to import specific definitions from a module. ## managing multiple installations How to manage multiple versions of the interpreter on the same machine, or the same interpreter with a different set of installed 3rd party packages. python: Virtualenv must be downloaded and installed. It is a tarball and it only works on Unix. ## list installed packages, install a package How to show the installed 3rd party packages, and how to install a new 3rd party package. perl cpanm is an alternative to cpan which is said to be easier to use. How to use cpan to install cpanm: $ sudo cpan -i App::cpanminus

How to install a module with cpanm:

$sudo cpanm Moose python Two ways to list both installed modules and modules from the standard library: $ pydoc modules
$python >>> help('modules') A Unix specific way to install a Python module that bypasses package management: $ tar xf libxml2-python-2.6.0.tar.gz $cd libxml2-python-2.6.0$ sudo python setup.py install

# Object Footnotes

## define class

php:

Properties (i.e. instance variables) must be declared publicprotected, or private. Methods can optionally be declared publicprotected, or private. Methods without a visibility modifier are public.

perl:

The heet shows how to create objects using the CPAN module Moose. To the client, a Moose object behaves like a traditional Perl object; it’s when a class needs to be defined that Moose code looks different from traditional Perl code.

• shortcuts for creating accessors and delegates
• attributes can be declared to a be a type
• classes are objects and can be reflected upon
• mixins, which are called roles

Moose objects are illustrated instead of Perl objects because Moose is used by the Catalyst web framework, and some of the features in Moose are in the Perl 6 object system. Here is how to define a class in the traditional Perl way:

package Int; sub new { my $class = shift; my$v = $_[0] || 0; my$self = {value => $v}; bless$self, $class;$self; } sub value { my $self = shift; if ( @_ > 0 ) {$self->{'value'} = shift; } $self->{'value'}; } sub add { my$self = shift; $self->value +$_[0]; } sub DESTROY { my $self = shift; my$v = $self->value; print "bye,$v\n"; }

python:

As of Python 2.2, classes are of two types: new-style classes and old-style classes. The class type is determined by the type of class(es) the class inherits from. If no superclasses are specified, then the class is old-style. As of Python 3.0, all classes are new-style.

New-style classes have these features which old-style classes don’t:

• universal base class called object.
• descriptors and properties. Also the __getattribute__ method for intercepting all attribute access.
• change in how the diamond problem is handled. If a class inherits from multiple parents which in turn inherit from a common grandparent, then when checking for an attribute or method, all parents will be checked before the grandparent.

## create object

How to create an object.

## get and set attribute

How to get and set an attribute.

perl:

Other getters:

$i->value()$i->{'value'}

Other setters:

$i->{'value'} =$v;

python:

Defining explicit setters and getters in Python is considered poor style. If it becomes necessary to extra logic to attribute, this can be achieved without disrupting the clients of the class by creating a property:

def getValue(self): print("getValue called") return self.__dict__['value'] def setValue(self,v): print("setValue called") self.__dict__['value'] = v value = property(fget=getValue, fset = setValue)

## instance variable accessibility

How instance variable access works.

## define method

How to define a method.

## invoke method

How to invoke a method.

perl:

If the method does not take any arguments, the parens are not necessary to invoke the method.

## destructor

How to define a destructor.

perl:

Perl destructors are called when the garbage collector reclaims the memory for an object, not when all references to the object go out of scope. In traditional Perl OO, the destructor is named DESTROY, but in Moose OO it is named DEMOLISH.

python:

A Python destructor is not guaranteed to be called when all references to an object go out of scope, but apparently this is how the CPython implementations work.

ruby:

Ruby lacks a destructor. It is possible to register a block to be executed before the memory for an object is released by the garbage collector. A ruby
interpreter may exit without releasing memory for objects that have gone out of scope and in this case the finalizer will not get called. Furthermore, if the finalizer block holds on to a reference to the object, it will prevent the garbage collector from freeing the object.

## method missing

How to handle when a caller invokes an undefined method.

php:

Define the method __callStatic to handle calls to undefined class methods.

python:

__getattr__ is invoked when an attribute (instance variable or method) is missing. By contrast, __getattribute__, which is only available in Python 3, is always invoked, and can be used to intercept access to attributes that exist. __setattr__ and __delattr__ are invoked when attempting to set or delete attributes that don’t exist. Thedel statement is used to delete an attribute.

ruby:

Define the method self.method_missing to handle calls to undefined class methods.

## inheritance

How to use inheritance.

perl:

Here is how inheritance is handled in traditional Perl OO:

package Counter; our @ISA = "Int"; my $instances = 0; our$AUTOLOAD; sub new { my $class = shift; my$self = Int->new(@_); $instances += 1; bless$self, $class;$self; } sub incr { my $self = shift;$self->value($self->value + 1); } sub instances {$instances; } sub AUTOLOAD { my $self = shift; my$argc = scalar(@_); print "undefined: $AUTOLOAD " . "arity:$argc\n"; }

## invoke class method

How to invoke a class method.

# PHP

PHP Manual
General Style and Syntax Codeigniter
Coding Standards Pear
PHP Style Guide Apache

The PHP interpreter is packaged in 3 different ways: (1) as a standalone executable which can be executed as a CGI script, (2) as a dynamically linked library which adheres to the SAPI of a webserver such as Apache or IIS, and (3) as a standalone executable which can be used to run PHP scripts from the command line. The latter executable is called PHP C

From the perspective of a PHP programmer, there no important differences between PHP CGI and PHP SAPI. The programmer should be aware of the following differences between PHP CGI/SAPI and PHP CLI:

• PHP CGI/SAPI writes HTTP headers to standard out before any output specified by the program. PHP CLI does not.
• PHP CLI sets the constants STDIN, STDOUT, and STDERR. PHP CGI/SAPI do not.
• PHP CLI has no timeout. PHP CGI/SAPI will typically timeout a script after 30 seconds.
• PHP CGI/SAPI add HTML markup to error messages. PHP CLI does not.
• PHP CLI does not buffer output, so calling flush is never necessary. PHP CGI/SAPI buffer output.

# Perl

The first character of a perl variable ($, @, %) determines the type of value that can be stored in the variable (scalar, array, hash). Using an array variable (@foo) in a scalar context yields the size of the array, and assigning scalar to an array will set the array to contain a single element.$foo[0] accesses the first element of the array @foo, and $bar{‘hello’} accesses the value stored under ‘hello’ in the hash %bar.$#foo is the index of the last element in the array @foo.

Scalars can store a string, integer, or float. If an operator is invoked on a scalar which contains an incorrect data type, perl will always perform an implicit conversion to the correct data type: non-numeric strings evaluate to zero.

Scalars can also contain a reference to a variable, which can be created with a backslash: $baz = \@foo; The original value can be dereferenced with the correct prefix: @$baz. References are how perl creates complex data structures, such as arrays of hashes and arrays of arrays. If $baz contains a reference to an array, then$baz->[0] is the first element of the array. if $baz contains a reference to a hash,$baz->{‘hello’} is the value indexed by ‘hello’.

The literals for arrays and hashes are parens with comma separated elements. Hash literals must contain an even number of elements, and ‘=>’ can be used in placed of a comma ‘,’ between a key and its value. Square brackets, e.g. [ 1, 2, 3 ], create an array and return a reference to it, and curly brackets, e.g. { ‘hello’ => 5, ‘bye’ => 3 }, create a hash and return a reference to it.

By default perl variables are global. They can be made local to the containing block with the my or the local keyword. my gives lexical scope, and local gives dynamic scope. Also by default, the perl interpreter creates a variable whenever it encounters a new variable name in the code. The ‘use strict;’ pragma requires that all variables be declared with my, local, or our. The last is used to declare global variables.

perl functions do not declare their arguments. Any arguments passed to the function are available in the @_ array, and the shift command will operate on this array if no argument is specified. An array passed as an argument is expanded: if the array contains 10 elements, the callee will have 10 arguments in its @_ array. A reference (passing \@foo instead of @foo) can be used to prevent this.

Some of perl’s special variables:

• : pid of the perl process
• $0: name of the file containing the perl script (may be a full pathname) •$@: error message from last eval or require command
• $&,$, $’: what last regex matched, part of the string before and after the match •$1..\$9: what subpatterns in last regex matched

# Python

2.7: LanguageStandard Library
Why Python3 Summary of Backwardly Non-compatible Changes in Python 3
3.2: LanguageStandard Library
PEP 8: Style Guide for Python Code van Rossum

Python uses leading whitespace to indicate block structure. It is not recommended to mix tabs and spaces in leading whitespace, but when this is done, a tab is equal to 8 spaces. The command line options ‘-t’ and ‘-tt’ will warn and raise an error respectively when tabs are used inconsistently for indentation.

Regular expressions and functions for interacting with the operating system are not available by default and must be imported to be used, i.e.

import re, sys, os

Identifiers in imported modules must be fully qualified unless imported with from/import:

from sys import path from re import *`

There are two basic sequence types: the mutable list and the immutable tuple. The literal syntax for lists uses square brackets and commas [1,2,3] and the literal syntax for tuples uses parens and commas (1,2,3).

The dictionary data type literal syntax uses curly brackets, colons, and commas { “hello”:5, “goodbye”:7 }. Python 3 adds a literal syntax for sets which uses curly brackets and commas: {1,2,3}. Dictionaries and sets are implemented using hash tables and as a result dictionary keys and set elements must be hashable.

All values that can be stored in a variable and passed to functions as arguments are objects in the sense that they have methods which can be invoked using the method syntax.

Attributes are settable by default. This can be changed by defining a __setattr__ method for the class. The attributes of an object are stored in the __dict__ attribute. Methods must declare the receiver as the first argument.

Classes, methods, functions, and modules are objects. If the body of a class, method, or function definition starts with is a string, it is available available at runtime via __doc__. Code examples in the string which are preceded with ’>>>’ (the python repl prompt) can be executed by doctest and compared with the output that follows.

# Ruby

Ruby consistently treats all values as objects. Classes are objects. Methods, however, are not objects. The system provided classes are open: i.e. the user can add methods to String, Array, or Fixnum. Another difference from python (and perl) is that ruby only permits single inheritance. However, ruby modules are mix-ins and can be used to add methods to a class via the include statement. Ruby methods can be declared private, and this is enforced by the interpreter.

In ruby, there is no difference between named functions and methods: top level ‘functions’ are in fact methods defined on the main object. All methods have a receiver which can be referenced with the self: the receiver does not needed to be treated as an argument like in perl and python.

Ruby has syntax which aids functional programming: a ruby method invocation can be followed by a block, which is a closure. The invoked method can call the block with yield(arg1,…). Also, the invoked method can store the block in a variable if its signature ends with &<varname>. The block can then be invoked with <varname>.call(arg1,…).