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 -a offers a different style of interactive mode. 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.

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 encounters 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.

Colons are also used to separate keys from values in dictionaries and in sequence slice notation.

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 def, class, or module.

Curly brackets are also used for hash literals, and the #{ } notation is used to interpolate expressions into strings.

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.

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.

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.

A list of regions which define a lexical 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 scope 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 if, unless, while, until, for, 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 if, unless, case, while, 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:

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:

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:

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 the global 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 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 individually 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.

Capitalized variables contain constants and class/module names. By convention, constants are all caps and class/module names are camel case. The ruby interpreter does not prevent modification of constants, it only gives a warning. Capitalized variables are globally visible, but a full or relative namespace name must be used to reach them: e.g. Math::PI.

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 (=) when an equality test (==) was intended. 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.

The C-style increment and decrement operators can be used to increment or decrement values. They return values and thus can be used in expressions. The prefix versions return the value in the variable after mutation, and the postfix version return the value before mutation.

Incrementing a value two or more times in an expression makes the order of evaluation significant:

Any identifier which matches TRUE case-insensitive can be used for the TRUE boolean. Similarly for FALSE.

In general, PHP variable names are case-sensitive, but function names are case-insensitive.

When converted to a string for display purposes, TRUE renders as "1" and FALSE as "". The equality tests TRUE == 1 and FALSE == "" evaluate as TRUE but the equality tests TRUE === 1 and FALSE === "" evaluate as FALSE.

Equality, inequality, greater than, less than, greater than or equal, less than or equal.

php:

Most of the relational operators will convert a string to a number if the other operand is a number. Thus 0 == "0" is true. The operators === and !== do not perform this conversion, so 0 === "0" is false.

perl:

The operators: == != > < >= <= convert strings to numbers before performing a comparison. Many string evaluate as zero in a numeric context and are equal according to the == operator. To perform a lexicographic string comparison, use: eq, ne, gt, lt, ge, le.

python:

Relational operators can be chained. The following expressions evaluate to true:

1 < 2 < 3
1 == 1 != 2

In general if Ai are expressions and opi are relational operators, then

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.

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.

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.

Barewords are strings without quote delimiters. They are a feature of shells. Barewords cannot contain whitespace or any other character used by the tokenizer to distinguish words.

Before Perl 5 subroutines were invoked with an ampersand prefix & or the older do keyword. With Perl 5 neither is required, but this made it impossible to distinguish a bareword string from a subroutine without knowing all the subroutines which are in scope.

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:

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"

The \uhhhh escapes are also available inside Python 2 Unicode literals. Unicode literals have a u prefiix:

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.

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 with the delimiters preserved as separate elements; how to split a string into an array of single character strings.

The return value of the =~ operator is boolean, indicating whether a match occurred. Also the left hand side of the =~ operator must be a scalar variable that can be modified. Using the =~ operator is necessarily imperative, unlike the Text::Trim functions which can be used in expressions.

PHP converts a scalar to the desired type automatically and does not raise an error if the string contains non-numeric data. If the start of the string is not numeric, the string evaluates to zero in a numeric context.

perl:

Perl converts a scalar to the desired type automatically and does not raise an error if the string contains non-numeric data. If the start of the string is not numeric, the string evaluates to zero in a numeric context.

python:

float and int raise an error if called on a string and any part of the string is not numeric.

ruby:

to_i and to_f always succeed on a string, returning the numeric value of the digits at the start of the string, or zero if there are no initial digits.

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.

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 characterRuby: hex digit

PHP, Perl: [ \t]Ruby: [0-9a-fA-F]

\H

PHP, Perl: not a horizontal whitespace characterRuby: 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. They 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 them 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 minimum 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.

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.

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 noun datetime 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:

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 tm struct in Python:

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.

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.

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.

python:

The Python datetime object created by now() and utcnow() has no timezone information associated with it. The strftime() method assumes a receiver with no timezone information represents a local time. Thus it is an error to call strftime() on the return value of utcnow().

Here are two different ways to get the current Unix epoch. The second way is faster:

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 date command.

php:

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

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.

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:

The Time class has a zone method which returns the time zone abbreviation for the object. There is a tzinfo gem which can be used to create timezone objects using the Olson database name. This can in turn be used to convert between UTC times and local times which are daylight saving aware.

How to get the microseconds component of a combined date and time value. The SI abbreviations for milliseconds and microseconds are ms and μs, respectively. The C standard library uses the letter u as an abbreviation for micro. Here is a struct defined in /usr/include/sys/time.h:

Python has the mutable list and the immutable tuple. Both are sequences. To be a sequence, a class must implement __getitem__, __setitem__, __delitem__, __len__, __contains__, __iter__, __add__, __mul__, __radd__, and __rmul__.

ruby:

Ruby provides an Array datatype. If a class defines an each iterator and a comparison operator <=>, then it can mix in the Enumerable module.

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:

Taking a reference is customary way to make an address copy in Perl, but the Perl example is not equivalent to the other languages in that different syntax has to be used to access the original array and the address copy: @a and @$a1. To make @a1 and @a refer to the same array, use typeglobs:

*a1 = *a;

python:

The slice operator can be used to make a shallow copy:

a2 = a[:]

list(v) always returns a list, but v[:] returns a value of the same as v. The slice operator can be used in this manner on strings and tuples but there is little incentive to do so since both are immutable.

copy.copy can be used to make a shallow copy on types that don't support the slice operator such as a dictionary. Like the slice operator copy.copy returns a value with the same type as the argument.

How to test whether a condition holds for all members of an array; how to test whether a condition holds for at least one member of any array.

A universal test is always true for an empty array. An existential test is always false for an empty array.

A existential test can readily be implemented with a filter. A universal test can also be implemented with a filter, but it is more work: one must set the condition of the filter to the negation of the predicate and test whether the result is empty.

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.

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.

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.

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.

The three common methods of parameter passing are pass by value, pass 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.

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.

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:

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.

ruby:

The following lambda and Proc object behave identically:

sqr = lambda { |x| x * x }
sqr = Proc.new {|x| x * x }

With respect to control words, Proc objects behave like blocks and lambdas like functions. In particular, when the body of a Proc object contains a return or break statement, it acts like a return or break in the code which invoked the Proc object. A return in a lambda merely causes the lambda to exit, and a break inside a lambda must be inside an appropriate control structure contained with the lambda body.

If a variable containing a string is used like a function then PHP will look for a function with the name in the string and attempt to invoke it.

python:

Python function are stored in variables by default. As a result a function and a variable with the same name cannot share the same scope. This is also the reason parens are mandatory when invoking Python functions.

A decorator replaces an invocation of one function with another in a way that that is imperceptible to the client.

Normally a decorator will add a small amount of functionality to the original function which it invokes. A decorator can modify the arguments before passing them to the original function or modify the return value before returning it to the client. Or it can leave the arguments and return value unmodified but perform a side effect such as logging the call.

A 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

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 CLI.

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.

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 a scalar to an array will modify 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 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 the => operator 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 keyword 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

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}. This notation is also available in Python 2.7. 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 has a special type of value called a symbol. The literal syntax for a symbol is :identifier or :"arbitrary string". The methods to_s and to_sym can be used to convert symbols to strings and strings to symbols. Symbols can be used to pass functions or methods as arguments by name. They can be used as keys in Hash objects in place of strings, but the client must remember the type of the keys since :foo != "foo". Also note that converting a Hash object with symbols as keys to JSON and then back will yield a Hash object with strings as keys.

In Ruby 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. Moreover classes are objects. The system provided classes are open and as a result the user can add methods to classes such as String, Array, or Fixnum. Ruby only permits single inheritance, but 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. Object attributes are private by default and attribute names have an ampersand @ prefix. The methods attr_reader, attr_writer, and attr_accessor can be used in a class block to define a getter, setter, or both for an attribute.

When invoking a method the parens are optional. If there are two or more arguments they must still be separated by commas. If one of the arguments is an expression containing a method invocation with arguments, then the Ruby interpreter will assign as many arguments as possible to the innermost method invocation.

Inside a Ruby method, the self keyword refers to the receiver. It is not declared when defining the method. Ruby functions are implemented as methods on an object called main which has the special property that any methods defined on it become instance methods in the Object class which is a base class of most Ruby objects. This makes the method available everywhere. Methods defined at the top level are also added to the main object and the Object class. Functions which Ruby provides by default are instance methods defined the Object class or the Kernel module.

Ruby methods are not objects and cannot directly be stored in variables. It is worth emphasizing that the Python interpreter when encountering a method identifier with no parens returns the method as an object value, but the Ruby interpreter invokes the method. As mentioned earlier, methods can be passed by name using symbols. If a method receives a symbol representing a method as an argument, it can invoke the method with the syntax :symbol.to_proc.call(args…). Note that to_proc resolves the symbol to the method that is in scope where it is invoked.

Although passing a method or a function is a bit awkward, Ruby provides a convenient mechanism called blocks for simultaneously defining an anonymous function at the invocation of a method and providing it to the method as an argument. The block appears immediately after the closing paren of the method invocation and uses either the { |args…| body } or do |args…| bodyend syntax. The invoked method can in turn invoke the block with the yield keyword.

Ruby blocks are closures like lambda functions and can see local variables in the enclosing scope in which they were defined. In Ruby 1.8 the arguments of the block are local to the block unless the variables were already in use in the enclosing scope. In Ruby 1.9 this was changed so that block arguments are always local to the block. In addition Ruby 1.9 add semicolon syntax so that identifiers listed after the arguments could be made local to the block even if already defined in the containing scope.

The lambda keyword or the Proc.new constructor can be used to store an anonymous function in a variable. The function can be invoked with variable.call(). If such a function is passed to a method argument as the last argument and preceded with an ampersand, the function will be used as the block for the method. Conversely, if the last argument in a method definition is preceded with an ampersand, any block provided to the function will be bound to the argument name as an anonymous function.