Perl Over PHP!org

In 1987 Larry Wall created the scripting language Perl, with the motto “there is more than one way to do it” (TIMTOWTDI).

Perl quickly became the language of choice among hobbyist programmers as well as among professional ones, because of its powerful text processing capabilities, simplicity and straight-forwardness, while maintaining and following a clear standard.

In 1995 Rasmus Lerdorf shared his Personal Home Page scripting language, PHP, insprired by Perl.

Hobbyist home page creators picked up PHP because of its gentle learning curve and easy integration with Apache and since then, Lerdorf’s personal set of tools have grown immensely popular as well as more mature.

After time when more and more people wanted a presence online, PHP replaced Perl as the most popular server-side scripting language, mainly because of the many hosts that liked the tight integration with Apache and chose to support it.

Today, most hosts support PHP for their web hosting services. But PHP has many issues, most of which we’ll go through below, and we will try to convince you to choose Perl for your company’s website, your personal home page or even just your hobby project.

Why Choose Perl?

To convince you that Perl is good and PHP is bad, we will compare the two languages, and at the end of this text, I hope you will consider Perl, instead of PHP, the next time you start a project involving anything.

The following problems of PHP are taken from “PHP in contrast to Perl”:

Arguments and return values are extremely inconsistent: it is impossible not to be confused. I have experienced similar confusion with many PHP functions. Sometimes the order is $array, $element, other times it’s the other way around.

PHP has seperate functions for case insensitive operations: this adds to the confusion. It’s hard to remember all the functions just because there are so many differently named variants of them. Perl, on the other hand, solves this more elegantly and the two solutions are global, so it’s never more than two things to remember.

Perl: $foo cmp $bar                            lc $foo cmp lc $bar
PHP:  strcmp($foo, $bar)                       strcasecmp($foo, $bar)

Perl: index($foo, $bar)                        index(lc $foo, lc $bar)
PHP:  strpos($foo, $bar)                       stripos($foo, $bar)

Perl: $foo =~ s/foo/bar/                       $foo =~ s/foo/bar/i
PHP:  $foo = str_replace('foo', 'bar', $foo)   $foo = str_ireplace(...)
PHP:  $foo = ereg_replace('foo', 'bar' ,$foo)  $foo = eregi_replace(...)

PHP has inconsistent function naming: even more confusion. There seems to be no logic for naming functions:

  • Case insensitive functions have the i or case at different positions in their names.
  • There is no apparent system in underscore(s) versus no underscore(s). Perl has no core function names with underscores, whereas in PHP:
UNDERSCORE               NO UNDERSCORE:

stream_get_line          readline
disk_free_space          diskfreespace
is_object                isset
mcal_day_of_week         jddayofweek
set_error_handler        setlocale
snmp_get_quick_print     snmpget
get_browser              getallheaders
base64_encode            urlencode
image_type_to_mime_type  imagetypes
msql_num_fields          mysql_numfields
php_uname                phpversion
strip_tags               stripslashes
bind_textdomain_codeset  bindtextdomain
cal_to_jd                gregoriantojd
str_rot13                strpos
  • PHP has unlink, link and rename (system calls), but touch (the system call is utime, not touch).
  • And they can't decide on word order:
    Object verb: base64_decode, iptcparse, str_shuffle, var_dump
    Verb object: create_function, recode_string
    Perl core functions are all “verb object” except the superseded dbm* functions. (Note that sys is a prefix, not an object. And that flock and lstat were named after the system calls. shm* and msg* are library calls.)

PHP has no lexical scope: Perl has lexical scope and dynamic scope, none of which PHP has. For an explanation of why lexical scope is important, see Coping with Scoping

PHP has too many functions in the main namespace: PHP has 3079 main functions, whereas Perl only has 206. The mean PHP function name length is 13.67, whereas the mean Perl function name length is only 6.22. The reason for Perl having fewer functions is, that Perl has short syntax equivalents for some functions, and another reason we’ll get to shortly.

readpipe('ls -l') ==> `ls -l`
glob('*.txt')     ==> <*.txt>
readline($fh)     ==> <$fh>
quotemeta($foo)   ==> "\Q$foo"
lcfirst($foo)     ==> "\l$foo"  (lc is \L)
ucfirst($foo)     ==> "\u$foo"  (uc is \U)

PHP lacks abstraction and takes TIMTOWTDI to bad extremes: The other reason for the low function count in Perl is, that PHP usually has several functions that are very similar. In Perl, you have to know and remember less. Perl also makes extensive use of modules, especially the DBI module which provides support for SQL databases, instead of bloating the core like PHP with features that occupy memory but are rarely used.

* Escaping:

    * PHP: (14)
    dbx_escape_string, escapeshellarg, escapeshellcmd, pg_escape_bytea,
    pg_escape_string, pg_unescape_bytea, addslashes, addcslashes, preg_quote,
    quotemeta, mysql_escape_string, mysql_real_escape_string,
    mysqli_real_escape_string, sqlite_escape_string

    * Perl: (2) [1]
    quotemeta, $dbh->quote

* Sorting:

    * PHP: (16)
    sort, arsort, asort, krsort, ksort, natsort, natcasesort, rsort, usort,
    array_multisort, uasort, uksort, dbx_sort, imap_sort, ldap_sort, yaz_sort

    * Perl: (1)
    sort

* Walking a list

    * PHP: (10)
    array_filter, preg_grep, array_search, array_unique, in_array, array_map,
    array_walk, array_count_values, array_change_key_case, array_sum

    * Perl: (2)
    map, grep

* Splitting:

    * PHP: (8)
    split, explode, strtok, spliti, chunk_split, mb_split, preg_split,
    str_split

    * Perl: (1)
    split

* Matching:

    * Strings:

        * PHP: (11)
        strstr, strchr, stristr, strpos, strrchr, stripos, mb_strpos,
        mb_strrpos, strrpos, strripos, substr

        * Perl: (3)
        index, rindex, substr

    * Regular expressions:

        * PHP: (6)
        ereg, eregi, mb_ereg, mb_eregi, preg_match, preg_match_all

        * Perl: (1)
        m//

* Substituting a matched part:

    * PHP: (12)
    ereg_replace, eregi_replace, mb_ereg_replace, mb_eregi_replace,
    preg_replace, str_ireplace, str_replace, ltrim, rtrim, trim, nl2br

    * Perl: (1)
    s///

* Connecting to an SQL database:

    * PHP: (17)
    dbx_connect, fbsql_connect, ibase_connect, msql_connect, msql_pconnect,
    mssql_connect, mysql_connect, odbc_connect, pg_connect, pg_pconnect,
    sesam_connect, ifx_pconnect, ifx_connect, sqlite_open, sqlite_popen,
    mysqli_connect, mysqli_pconnect

    * Perl: (2)
    DBI->connect, DBI->connect_cached

* Opening:

    * PHP: (5)
    dio_open, fopen, proc_open, popen, gzopen[2]

    * Perl: (2)
    open, sysopen

* Reading/receiving:

    * PHP: (12)
    dio_read, fread, gzread[2], socket_read, socket_recv, socket_recvfrom,
    socket_recvmsg, readline, fgetc, fgets, stream_get_line, file

    * Perl: (5)
    read, readline, sysread, recv, getc

* Printing/writing:

    * PHP: (14)
    print, echo, printf, fprintf, vprintf, dio_write, fwrite, fputs,
    gzwrite[2], socket_send, socket_sendmsg, socket_sendto, socket_write,
    socket_writev

    * Perl: (5)
    print, printf, syswrite, send, write

* Closing:

    * PHP: (7)
    closelog, dio_close, fclose, gzclose[2], pclose, socket_close,
    proc_close

    * Perl: (1)
    close

* Miscellaneous:

    * PHP:
    array_combine, array_fill, array_merge, list, range, count,
    create_function, strtr, pow, putenv, getenv, getmygid, getmypid, getmyuid

    * Perl:
    syntax or magic variables

[1] Because of system LIST syntax and DBI's placeholders, explicit escaping is usually not even needed.
[2] Handled in Perl by a PerlIO layer

But what about PHP 5? I recommend tnx.nl's document “PHP 5: not even close” on the subject.

Support Perl Over PHP? Extend the list! (GitHub account needed.)

To be added:

  • CGI website templates for easy deployment

I’m convinced—where do I start?

To begin, read O’Reilly’s A Beginner’s Introduction to Perl Web Programming. Then return here in a few weeks for more information and templates to help you with your Perl web programming.

We’re glad to have you on board!

Spread this website: Twitter