Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^4: Is Perl a good career move?

by Juerd (Abbot)
on May 24, 2005 at 14:58 UTC ( #460015=note: print w/replies, xml ) Need Help??

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

Heck, it doesn't even have anonymous functions.
Yes it does. (...) create_function.

Functions created by create_functions are NAMED, not anonymous. In fact, create_function RETURNS the name.

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

I sure do. If in "bar() ? bar() : $foo", the bar function does something impure, like increase a counter, it does so twice, while with Perl's "bar() || $foo", it is done only once. Also, if bar() takes 10 seconds to finish, then "bar() ? bar() : $foo" will take at least 20.

Juerd # { site => '', plp_site => '', do_not_use => 'spamtrap' }

Replies are listed 'Best First'.
Re^5: Is Perl a good career move?
by thinker (Parson) on May 24, 2005 at 15:46 UTC

    >> Functions created by create_functions are NAMED, not anonymous. In fact, create_function RETURNS the name.

    Create function does indeed return a string, but does that mean it is not an anonymous function. perhaps we are concentrating too much on semantics here

    The documentation for the function states " create_function -- Create an anonymous (lambda-style) function". Perhaps more semantics. Nevertheless, building a dispatch table from the result of create_function works for me.

    I take on board your point about ||= for a function which has side effects



      The functions created by create_functions are named, not anonymous. The value returned by create_function is the name of the newly created function. This name begins with a nullbyte, but it is a global name anyhow. $foo = create_function(...); $foo(); works because $foo here is a symbolic reference. create_function uses eval and has many caveats. From PHP's source:

      sprintf(eval_code, "function " LAMBDA_TEMP_FUNCNAME "(%s){%s}", Z_STRV +AL_PP(z_function_args), Z_STRVAL_PP(z_function_code));
      The function has a name, and is thus not anonymous. PHP's documentation is outright false.

      Your dispatch table may work for you. But it is very inefficient and very ugly. You'd even be better off with a bunch of explicitly named functions and simple symbolic references. Back to square one of the programming mine field...

      Juerd # { site => '', plp_site => '', do_not_use => 'spamtrap' }

Re^5: Is Perl a good career move?
by Anonymous Monk on Jan 02, 2008 at 20:05 UTC
    ($foo = bar()) || ($foo = $baz); $foo = (($bar = bar()) ? $bar : $baz); The key is that the || casts the right hand side as a boolean, and php does not have a magic variable that gets set while you aren't looking.
      That is incredibly ugly, and no "magic variable that gets set" is even RELEVANT in this discussion.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://460015]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-05-29 12:58 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (101 votes). Check out past polls.