Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Zsh and Perl?



Jeff Solomon wrote:
> I'm very sorry if this question seems naive but I've dug a little
> through the zsh source code and it seems appropriate.

It's a perfectly reasonable question, and one that really demands a more
practical solution than it's got up to now (i.e. actually writing).

> Embed perl into zsh:
>     
>     Looking at the source, this looks like the most straightforward
>     thing to do. It seems easy to add a 'perl_eval' command that would 
>     evaluate a snippet of perl in a persistent interpreter. 

This certainly seems like the way to go, and it should be easy:  just
follow the `perlembed' instructions, but put the result in a zsh module
(look at a simple one with its own builtin like `stat' for guidance).

> But I
>     would want to add command into the zsh enviroment from perl so I
>     can type:
> 
> 	my_command my_arg1 my_arg2
> 
>     instead of
> 
> 	perl_eval 'my_command my_arg1 my_arg2'
> 
>     which means you'd have to add support for calling back into zsh
>     from perl.

You mean that `my_command' is a perl command, but is handled by the zsh
command line interface, and calls a perl subroutine?  That should be just
as easy, I don't see you need to call back into zsh --- unless the problem
is simply adding the perl command when a perl module is loaded.  The zsh
code for doing this is easy, just use addbuiltin().  Then you need to make
some zsh interface code visible from the perl interpreter you've just
built, and that I can't visualize without some hands-on experience.  Doing
it this way round usually means creating a library with some xs code, which
would be the wrong way round for embedding perl in zsh.  A quick glance at
the perlembed manual page doesn't seem to help much.  You'd get more help
on this angle from the perl people.

> Embed zsh into perl:

Once you've solved the previous problem, you can actually sort of do this
too, by the same method.  Just make some code available which calls
something looking like bin_eval() (in builtin.c); then you have the effect
of a system() command without a fork.  If you want the full effect of
perl's `system', where a call with more than one argument is treated
specially, you have to work harder.

Any suitably general purpose module for doing this could be part of the
main distribution.  Ideally, in that case (and again, there may be other
issues), you might want ways of getting/setting perl variables via zsh
variables and that sort of thing.  The special variable code may well now
be powerful enough to handle this (make access go through perl_get_sv()
etc.), but a look at Src/Modules/parameter.c will show it's still pretty
hairy (though that's complicated by the fact it's intercepting the
associative array interface, which means an extra level of lookup).

-- 
Peter Stephenson <pws@xxxxxxxxxxxxxxxxx>       Tel: +39 050 844536
WWW:  http://www.ifh.de/~pws/
Dipartimento di Fisica, Via Buonarroti 2, 56127 Pisa, Italy



Messages sorted by: Reverse Date, Date, Thread, Author