简体   繁体   English

如何将IO发送到Erlang端口?

[英]How To Send IO To An Erlang Port?

I'm trying to hack together a little CLI for Windows and, inspired by James Smith's Elixirconf talk, I was trying to use a Port to drive the interaction. 我正在尝试为Windows编写一个小CLI,并且受到James Smith的Elixirconf谈话的启发,我试图使用端口来推动交互。 So this is what I tried: 所以这就是我的尝试:

Interactive Elixir (0.14.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> p = Port.open({:spawn,"clip"},[:stderr_to_stdout, :in, :exit_status])
#Port<0.1048>
iex(2)> send(p,"This is a test")
"This is a test"

The "clip" utility simply takes whatever is piped into it and puts it on Windows clipboard. “剪辑”实用程序只需将管道中的任何内容放入其中并将其放在Windows剪贴板上。 If you were to call it from the command line, you'd call it like this 如果你是从命令行调用它,你会这样称呼它

dir | clip

So as far as I can tell nothing's getting to the clipboard via my test. 所以据我所知,没有什么可以通过我的测试进入剪贴板。 I just wanted to ask if I'm sending the input to the stdio in the right fashion--I mean do I want to use send for that? 我只想问我是否以正确的方式将输入发送到stdio - 我的意思是我是否想使用send?

Interestingly if I do this: 有趣的是,如果我这样做:

p = Port.open({:spawn, "clip"}, [:stderr_to_stdout, :exit_status]) #Note no :in parameter
send(p,"This is a test")

I get this: 我明白了:

** (EXIT from #PID<0.45.0>) :badsig

I can't tell if the IO is actually getting sent to clip or not but I just wanted to confirm I'm doing the command correctly. 我无法判断IO是否实际上被发送到clip但我只是想确认我正在执行命令。

Windows 7 SP1 Windows 7 SP1

You don't need :in and you need to pass to the port {:stream, :binary} to be able to send to it bare strings. 您不需要:in并且您需要传递到端口{:stream, :binary}以便能够向其发送裸字符串。

Also try using port_command instead of sending to the port pid. 还尝试使用port_command而不是发送到端口pid。 http://www.erlang.org/doc/man/erlang.html#port_command-2 http://www.erlang.org/doc/man/erlang.html#port_command-2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM