RE: Re: Perl extension

php.internals

david

22 years ago
Dmitry, there are a couple of reasons why i am going away with the: perl_eval('@x = 1..10'); var_dump(perl_var('@x')); approach: 1. i don't want to force the users to use 2 statments just to get a single value out. 2. some variables don't have names and i want my users to be able to do stuff like: print_r(p2p_eval('[1..10]')); print_r(p2p_eval('{a => 1,b => 2}')); if pecl/perl already does that then we should encourage users to use the single statement notation for short Perl expressions. function and method calls are handled similiarly and it's the context within Perl that controls the context of the final variables returned back to PHP. for example: $k = p2p_eval('use Foo; $x = Foo->new; scalar $x->method'); will return a scalar to $k but: $k = p2p_eval('[$x->method]'); will return an array to $k and $k = p2p_eval('{$x->method}'); will return a hash to $k. note the package Foo is only loaded once and "remembered" so it doesn't need to be loaded again. the reason why i chose this method: 1. i don't want to introduce new syntax to the PHP language. i think someone suggest something like: $x->scalar->method; // for scalar context $x->array->method; // for array context $x->hash->method; // for hash context i think this will complicate the langauge down the road. also, in void context, you need to: $x->void->method; // which doesn't "look" right. 2. most Perl programmers are already familiar with the notation of: p2p_eval('[...]'); // array p2p_eval('{...}'); // hash p2p_eval('$x'); // scalar becaue the [],{}, and $ makes it clear what "things" are returned. for function calls, i also have another method named p2p_map that does something like: $v = p2p_eval('sub{[$_[0]+1, $_[1]+2, $_[2]+3, $_[3]+4]}',array(1,2,3,4)); print_r($v); prints: Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 7 ) the second argument is supplied to the first argument (which is a code reference) and consequencely become the input of the Perl function. again, the result is returned base on the context within the Perl code, PHP doesn't (and shouldn't) care. my extension still have a lot of problem that needs to be solved, for example, Perl glob, I/O handle and some complicated data structures aren't handled correctly. unfortunately, i do not have a lot of time to work on it (i did it for fun) but i want to share my experience with you so maybe you can think about some of the stuff that i did and have some more ideas. thanks! david -----Original Message----- From: Dmitry Stogov [mailto:d.stogov@gmx.net] Sent: Friday, March 05, 2004 1:56 AM To: David Zhuo Cc: internals@lists.php.net Subject: RE: [PHP-DEV] Re: Perl extension pecl/perl can work in the simular way. It don't return value in same context as latest assignment (like p2p_eval does), but it allows to get perl variable's value. <?php perl_eval('$x = 10'); var_dump(perl_var('$x')); perl_eval('@x = 1..10'); var_dump(perl_var('@x')); perl_eval('%x = 1..10'); var_dump(perl_var('%x')); $k = perl_eval("'x1234y' =~ /(\d+)/; $1"); var_dump($k); ?> I will think about your way to determine return context for perl_eval, but unfortunately it can't be used for functions and methods calls. Dmitry.