ocaml
How to use OCaml as a script language like bash and python? It will be good that I can write a script in OCaml and directly invoke it.
如何使用OCaml作为bash和python等脚本语言? 我可以在OCaml中编写脚本并直接调用它会很好。
For example, I write a script named “script.ml” and then I can invoke it directly by
例如,我编写了一个名为“ script.ml”的脚本,然后可以通过直接调用它
$ ./script.ml
Thanks!
谢谢!
Three helloworld scripts:
三个helloworld脚本:
script3.ml
script3.ml
#! /usr/bin/env ocaml
print_string "Hello world!n";;
script1.ml:
script1.ml:
#! /bin/ocaml
print_string "Hello world!n";;
script2.ml
script2.ml
#! /bin/sh
# (*
exec ocaml "$0" "$@"
*) use "topfind";;
print_string "Hello world!n";;
Idea from findlib quickstart.
来自findlib quickstart的想法。
Remember to make the executable by
记得通过以下方式使可执行文件
chmod 755 script.ml
I prefer script3.ml. The script1.ml is straightforward, but it is bounded to /bin/ocaml
for the ocaml command. The script2.ml is more complex, but use your environmental configuration for the location of the ocaml command.
我更喜欢script3.ml。 script1.ml很简单,但是对于ocaml命令,它已绑定到/bin/ocaml
。 script2.ml较为复杂,但是请使用您的环境配置作为ocaml命令的位置。
Also be aware that the overall source code is compiled in total first and then is executed. Hence, no execution happens if there is some error, such as failures to compile the script. This is different from a bash script that will be executed by the interpreter until there is an error happens.
还应注意,总体源代码将首先全部编译,然后再执行。 因此,如果存在某些错误 (例如无法编译脚本),则不会执行任何操作。 这与解释器将在错误发生之前执行的bash脚本不同。
There are other discussions and tools on using OCaml as a scripting language:
关于使用OCaml作为脚本语言还有其他讨论和工具 :
OCaml as a scripting language.
ocamlscript: natively-compiled OCaml scripts.
OCaml是一种脚本语言 。
ocamlscript:本机编译的OCaml脚本 。
Most preferred script:
最喜欢的脚本:
$ cat script3.ml
#! /usr/bin/env ocaml
print_string "Hello world!n";;
翻译自: https://www.systutorials.com/how-to-use-ocaml-as-a-script-language/
ocaml