http://qs321.pair.com?node_id=459926


in reply to Re^2: Is Perl a good career move?
in thread Is Perl a good career move?

Hi Juerd,

You make some excellent points about PHP, (and having spent much of the last 2 years as a PHP programmer, I agree with many of your points). In particular the namespace issue is a killer (although using OO techniques you can avoid the problem to a degree). You have also made some errors in your comments.

>>> Heck, it doesn't even have anonymous functions.

Yes it does.

>>> That's another thing: how will you be rewriting that hash of coderefs? A hash of strings that are evaled at runtime?

#!/usr/bin/php -q <?php error_reporting(E_ALL); $dispatch_table = array( # create some anonymous functions 'foo' => create_function('$a', 'return "foo got $a\n";'), 'bar' => create_function('$a', 'return "bar got $a\n";'), 'baz' => create_function('$a', 'return "baz got $a\n";') ); print $dispatch_table['foo']('hello'); print $dispatch_table['bar']('hi'); print $dispatch_table['baz']('yo'); ?> # outputs foo got hello bar got hi baz got yo

>>> Also, don't think $foo = bar() || $baz does the same in PHP. In PHP, you end up with true or false. So you must write it in two separate expressions.

$baz = "DEFAULT VALUE"; function bar(){return "Assigned value";} function quux(){return false;} $foo = bar() ? bar() : $baz; print "$foo\n"; $foo = quux() ? quux : $baz; print "$foo\n"; # outputs Assigned value DEFAULT VALUE

I'm not sure if you could consider that as two separate expressions, but it seems a concise enough idiom for me.

While PHP does not have closures, I have used static variables in functions to emulate some of the behaviours I have used closures for in Perl.

Perl remains, by far, my language of choice, but I thought it would only be fair to point these things out

cheers

thinker