Log in

No account? Create an account

Previous Entry | Next Entry

Need I Go On

Perl way:
my $dumb = sub {
        $csr->_debug("server_assoc: dumb mode: $_[0]");
        return undef;

PHP way:
$dumb = create_function('&$csr,$err', '$csr->debug("server_assoc: ' .
                            'dumb mode: $err"); return false;');

wtf, you have to write the entire function as a string!!


( 14 comments — Leave a comment )
Aug. 30th, 2005 06:26 pm (UTC)
Aug. 31st, 2005 04:00 am (UTC)
I really don't understand that. You have created a function where you don't need to.

PHP 2 : Perl 1 : David 0
Aug. 31st, 2005 07:59 pm (UTC)
No, you don't understand the concept of a closure.

Perl 1 : David 1 : Computer Science 1 : Josh 0
Aug. 31st, 2005 06:47 am (UTC)

Even better is what the PHP version creates. It actually returns a string containing the words “lambda_1”, which is the name of your (apparently anonymous!) function. Brilliant.

As for the other poster that didn't understand what this was for, it's intended to be used like return $dumb->("The bread was stale"), in order to return undef while simultaneously logging a message to the debug log. Constructs like this are used all the time in languages which actually have sensible lambda functions.

Aug. 31st, 2005 08:20 am (UTC)
It serves no purpose. Why not just log_to_the_deb_log();? Maybe something if it was used on something purposeful, it would make sense.
Aug. 31st, 2005 05:41 pm (UTC)

This is a more common scenario: (in Perl just because it's easier to type it out off the top of my head)

sub process_page {
    my ($request) = $_[0]; # First parameter is request context

    my $fail = sub {
        $request->set_response_status("500", "Internal Server Error");
        $logger->log_error($request, $_[0]);
        return "<p>".$_[0]."</p>"; # Error message in first parameter

    my $args = $request->url_args();
    unless (defined($args->{thingyid})) {
        return $fail->("No thingyid specified");
    my $thingyid = $args->{thingyid};
    unless (thingy_exists($thingyid)) {
        return $fail->("ThingyID not found");
    my $result = process_thingy($thingyid);
    unless (defined $result) {
        return $fail->("Thingy processing failed");
    return $result->as_string();

As you can no doubt see for yourself, the process of failing only has to be written once, rather than having to repeat those two steps for each failure condition.

Out of interest, what's your angle on this? Perl hater or PHP fanatic? I can see plenty of reasons to dislike Perl, but not many reasons to love PHP. PHP has its place, but it's not really something to get fanatical about. If hating Perl is your angle, you should be aware that this feature is common in many scripting languages including JavaScript and Lua and even some of the more obscure statically-compiled languages such as Nice. Another common use for them is for event handlers; most languages implement them so that they run in the scope they were defined in, so they can reference local variables that were around at object creation time without them having to be explicitly stored in global variables. Of course, PHP doesn't have much call for event handlers due to its environment, so the utility of lambda functions is mostly limited to the above example, and in that case it doesn't take much refactoring to work around PHP's lack of (workable) support.

Regardless of all of this, I think David was more mocking the way PHP implements it than anything else. There are many languages without anonymous function support that do just fine, but a PHP developer obviously thought it was useful enough to implement it though not important enough to actually implement it properly.

Aug. 31st, 2005 08:01 pm (UTC)
Going to make two posts in succession because I don't have time to read over the Perl code. I am not a hater of either. I just think that there is really no reason to hate either of them when they both have equal functionality. David says "PHP teh sux0rs", but the only reason Perl coders say that is because it is different than what they normally use. It isn't that the functionality is lost, the syntax has changed.
Aug. 31st, 2005 08:03 pm (UTC)
Dude, you know PHP is the first scripting language I really learned. Moving to Perl I've just become very apprciative of the more robust things it has to offer that PHP doesn't. As Mart points out and I agree with, PHP isn't inheriently bad, just does a poor job of implementing some things.
Aug. 31st, 2005 08:10 pm (UTC)
/me still loves you ...

... (even if you did switch to Perl)

Yeah, Perl definitely handles some things like strings nicer.
Sep. 1st, 2005 06:12 pm (UTC)

All procedural programming languages are fundamentally the same as they are based on the same concepts. It's the syntax and extra constructs that set them apart.

In this case, though, the PHP implementation isn't functionally identical to the Perl implementation. Perl compiles the lambda function at compile time, while PHP does it at runtime. The most important upshot of this is that if you use PHP's create_function() in a loop it'll create a new function for each iteration, re-running the parser each time. In Perl, though, the function is compiled only once and each iteration merely creates a new closure, which is a pair containing a scope and an anonymous function. Also, Perl will garbage collect its lambda functions when they are no longer referenced but PHP cannot remove a function once it's created. The flaws in PHP's implementation are more than just syntax in this particular case, and it makes the functionality essentially useless.

I don't think it's hard to say that Perl beats PHP on this one. You might debate whether this functionality is useful at all, but surely you must concede that PHP's implementation is inferior.

Sep. 2nd, 2005 05:33 am (UTC)

Although, if you want to create a function that is to be compiled at runtime 'function FUNC_NAME()' is perhaps the best way to do so. You can also do eval() if create_function() doesn't suffice. eval() is probably compiled at runtime also.

So in David's list of PHP *vs* Perl debates, the score is tied.
Sep. 3rd, 2005 07:38 am (UTC)
You still don't get it
Sep. 3rd, 2005 01:55 pm (UTC)
Yes I do.
Sep. 4th, 2005 08:20 am (UTC)
Happy Birthday!!
( 14 comments — Leave a comment )