Checklist for PHP developer coding in Moonscript / LUA

If you have been a PHP developer in the past, and now wish to make a transition in moonscript / lua, you have come to the right place. Moonscript is a translator to LUA. LUA has shown better throughput performance in comparison to PHP. Please refer to Link for further information,

I managed a project in moonscript for my company. The request per second were close to 2000, and PHP wasn't able to scale up. Being familiar with PHP and Perl in the past, I did went through the transition to know the difference between both scriptings. I have already done the hard work, and wanted someone else to be benefited out of it. Hence, here are the key differences of Moonscript with respect to PHP.


General

  1. No semi colons needed.
  2. No curly braces needed.
  3. Whitespace sensitive. Section of code is delimited with line breaks and indentations.
  4. No multi line comment availability, like PHP /* */.
  5. -- (two minus) are used to mark a line as comment.

Data Structures

  1. Array index starting at 1 instead of 0.
  2. For creating global variable, use export instead of global.
  3. Arrays and Hashes are called Tables
  4. Arrays are declared differently. Values assignment in Arrays is done through : (colon), and not => (fat arrow). Curly braces are used instead of array(). Curly braces are also optional if only a single table is declared. New lines can be used instead of comma to delimit values.
  5. Array slicing is sophisticated. Offset is optional, takes start of array as default. Steps to jump can be specified, e.g. only retrieve in odd position, i.e. 1, 3, 5...N.

Looping Constructs

  1. No foreach () {}. for loop works both as for and foreach.

Conditional Constructs

  1. if statements can be used as an expression, e.g., print if then "" else "".
  2. unless statement is available.
  3. switch statement structure is different, i.e. switch when else.

String Processing

  1. .. (double period) used for string concatenation, instead of . (period).

Operators

  1. Operators or= and and= are available to do "$a = $a || $b and $a = $a && $b" respectively.
  2. ~= (tilde equal to) is available for not equal to comparison, in addition to != (exclamation equal to)

Functions

  1. Function declarations are very different. Uses = ->
  2. Functions without arguments can be called with an ! (exclamation).
  3. Function arguments does not need to be in parentheses, while calling.
  4. No parentheses tolerated between function name and opening parentheses, for function call.
  5. Functions implicitly return result of last statement.
  6. Functions can return multiple values. No need to collect them in arrays for returning.
  7. 0 is TRUE
  8. Expressions allowed for default values of functions, i.e. function a ($a=3, $b=1+2) {}
  9. Function calls allowed for default values of functions, i.e. function a ($a=3, $b=abc()) {}

Object Oriented Programming
  1. Class constructor function name is new
  2. No class destructor function.
  3. self is used instead of $this to refer to current object.
  4. @ and self. notation is used instead of $this-> or $self:: to refer to class variables and functions.
  5. Class member functions are called with \ (backslash), instead of -> (arrow)

Comments

Popular posts from this blog

504 Gateway Timeout on Amazon AWS ELB (Elastic Load Balancer)

AWS RDS incompatible-parameters solved

Sone ke gahno ki Bangalore mein adla badli (exchange)