Page 1 of 1

Howto embed newlisp in (shell) programs part 2

Posted: Sat Apr 05, 2008 9:35 am
by pjot
Hi,

Pending the issue of the previous post, I tried to solve it in a different way.

Similar to other core Unix programs, I tried to use a pipe to the STDIN of newLisp, but it fails. Below taken from the command prompt:
peter[~]$ echo "help" | awk '{print}'
help
peter[~]$ echo "help" | sed {}
help
peter[~]$ echo "help" | cut -f1
help
peter[~]$ echo "help" | newlisp -e "(println)"

nil
So how can I accept stdin to the newLisp binary and perform a task with it?

If newLisp wants to integrate in a Unix environment then it would be great if this works!

Thanks

Posted: Sat Apr 05, 2008 9:40 am
by Lutz
not necessary, see my answer to your previous post, we posted at the same time.

Posted: Sat Apr 05, 2008 9:44 am
by Lutz
and here is the solution to your pipe problem:

Code: Select all

~> echo "help" | newlisp -e "(read-line)"
"help"
~> 

Posted: Sat Apr 05, 2008 9:45 am
by pjot
Hi Lutz,

Indeed, the solution works fine!

However, just for arguments sake, how would you think about implementing a STDIN for newLisp? It would help a lot in different (Unix) situations if this is possible.

Just curious on your ideas about this.

Thanks
Peter

Posted: Sat Apr 05, 2008 9:46 am
by pjot
You're fast! It always worked, thanks a lot!

Complete list now:
peter[~]$ echo "help" | awk '{print}'
help
peter[~]$ echo "help" | sed {}
help
peter[~]$ echo "help" | cut -f1
help
peter[~]$ echo "help" | newlisp -e "(read-line)"
"help"
Peter

Posted: Sat Apr 05, 2008 9:54 am
by Lutz
In this chapter: http://www.newlisp.org/CodePatterns.html#scripts you find the basic pattern of a UNIX filter written in newLISP:

Code: Select all

~> echo "hello world" | ./filter
HELLO WORLD

~> cat filter 
#!/usr/bin/newlisp

(while (read-line) (println (upper-case (current-line))))

(exit)

~> 
'current-line' always contains the last result from 'read-line'. This pattern has been well tested in a UNIX environment. It also works with inetd for setting up servers.

ps: I am on your timezone ;-)

Posted: Sat Apr 05, 2008 2:04 pm
by pjot
Aha! I was already wondering why you were responding so fast ;-)

Maybe time for another European Newlisp Conference? ;-)

Posted: Sat Apr 05, 2008 3:00 pm
by Lutz
Maybe time for another European Newlisp Conference? ;-)
Sure!