因此,我了解Windows不支持Unix fork-exec模型,而是生成进程。但是,Strawberry Perl的fork仿真会产生带有负PID的子代。这些PID似乎是一致的,但是我不明白为什么它们是负数,或者说实际上是Perl如何模拟Unix fork。
use strict;
use warnings;
my $cpid = fork();
if ($cpid == 0) {
printf "%s\n", "I'm the child, pid is $$";
} else {
printf "%s\n", "I'm the parent, pid is $$, cpid is $cpid";
}
Run Code Online (Sandbox Code Playgroud)
这将产生类似于以下内容的信息:
I'm the parent, pid is 3428, cpid is -2600
I'm the child, pid is -2600
Run Code Online (Sandbox Code Playgroud) 根据关于Rosetta Code (源代码)的部分,OCaml元组(当由函数返回时)保持活动或作为一个单元收集.
元组的空间安全
OCaml程序员应该知道,当使用元组返回多个值时,终结不会独立处理每个值,而是作为整体处理元组.因此,只有当所有值都不可达时,才会最终确定所有值.
我想知道在OCaml 4.05中是否仍然如此,以及是否有任何其他方法可以获得不具有此属性的产品类型的相同功能.
我正在尝试从OCaml库中构建一个共享对象,我可以Python
使用该cdll
接口在其中运行ctypes
.
有没有办法在OCaml工具链中引导一些东西来生成一个链接在其中的依赖项的共享对象?
我开始怀疑OCaml根本不支持我正在尝试做的事情.我想调用一个OCaml函数,但是它会产生某种结果并将控制权返回到调用它的位置.
这是我的OCaml库
(* foo.ml *)
let add x y = x + y
Run Code Online (Sandbox Code Playgroud)
我用来在OS X上编译它的命令.(使用.so
而不是.dylib
略微违反平台的命名约定,但ocamlopt
只接受.o
或的扩展名.so
.)
$ ocamlopt -output-obj -fPIC -o foo.so foo.ml
Run Code Online (Sandbox Code Playgroud)
成功并产生foo.so
.
这是我第一次尝试将共享对象加载到python程序(python 3.6).
# foo.py
import ctypes
lib = ctypes.cdll.LoadLibrary('./foo.so')
print("Loaded")
Run Code Online (Sandbox Code Playgroud)
运行时会产生以下错误
摘抄:
self._handle = _dlopen(self._name, mode)
OSError: dlopen(./foo.so, 6): Symbol not found: _caml_code_area_end
Run Code Online (Sandbox Code Playgroud)
完整错误:
Traceback (most recent call last):
File "foo.py", line 3, in <module>
lib = …
Run Code Online (Sandbox Code Playgroud) 有没有一种方法die
可以$@
以不修改的方式进入Perl ?据我所知,在某些情况下会die
破坏内容$@
并"抛出",非本地转移控制.我只是想做后者.
何时die
是字符串,将$@
传递的异常对象的文件名和行号附加到die
字符串(或undef).
例如,
#!/usr/bin/env perl
# foo.pl
use strict;
use warnings;
use Data::Dumper;
eval { die '1'; };
my $hash_ref = {
msg => $@,
};
print Dumper($hash_ref);
Run Code Online (Sandbox Code Playgroud)
打印:
$VAR1 = {
'msg' => '1 at foo.pl line 7.
'
};
Run Code Online (Sandbox Code Playgroud)
但是如果参数不是字符串(或undef),则不会修改它.
#!/usr/bin/env perl
# foo2.pl
use strict;
use warnings;
use Data::Dumper;
eval { die ['string-inside-arrayref']; };
my $hash_ref = {
msg => $@,
}; …
Run Code Online (Sandbox Code Playgroud) 我正在寻找使用复杂键(任意引用)作为哈希键的解决方案,除了它们对键执行深度比较而不是参考比较.
我正在寻找一个函数,最好是在一个核心模块中,将任意引用(可能不包括子程序和类型地块引用)序列化为字符串,以便以后可以重构它们.
my @complex_key = qw[key1 key2];
my %hash;
$hash{serialize([@complex_key])} = 'value';
deserialize((keys %hash)[0]);
# should deeply equal [@complex_key]
Run Code Online (Sandbox Code Playgroud) 我正在弄乱OCaml FFI试图弄清楚如何推断C enum的宽度(我认为是C的实现定义)并且我试图插入一个错误宽度的类型来看看有什么打击在运行时.这就是动力,但我遇到的实际问题更为平凡.
我有一个简单的OCaml文件,它使用C FFI调用一个简单的函数example.c
,将枚举转换为int.
open Printf;;
let (@->) = Ctypes.(@->);;
let returning = Ctypes.returning;;
let foreign = Foreign.foreign;;
(* deliberately use the wrong scalar type for argument *)
let wrong_int64_of_color =
foreign "int_of_color" (Ctypes.int64_t @-> returning Ctypes.int64_t);;
let main () =
printf "%Ld\n" (wrong_int64_of_color (Int64.of_int 100));;
let () = main ();;
Run Code Online (Sandbox Code Playgroud)
我配置OPAM并安装Ctypes
和Ctypes.Foreign
% opam config env | sed -e 's/=.*/=/'
CAML_LD_LIBRARY_PATH=
OPAMUTF8MSGS=
MANPATH=
PERL5LIB=
OCAML_TOPLEVEL_PATH=
PATH=
% opam list | grep ctypes
ctypes 0.6.2 …
Run Code Online (Sandbox Code Playgroud) 我编写的大多数函数都很小,而且它们中的相对较少是递归的.Common Lisp是否通过"解除绑定"其体内函数的名称来提供一种防止递归的方法.或者,它是否提供了一种取消绑定符号的函数值的方法,以便我可以自己滚动显式非递归defun
?
我希望能够做类似以下的事情,可能隐藏在宏后面.
(defun non-recursive-func (arg)
(unflet (non-recursive-func)
(+ (first arg) (second arg))))
Run Code Online (Sandbox Code Playgroud)
我今天偶然写了一些有问题的代码,其中包装器函数委托给自己而不是unwrapped函数,这让我意识到使用一些编译时机制防止递归可能是有用的.
(defun fcompose-2 (f g)
(lambda (x) (funcall f (funcall g x))
(defun fcompose (&rest args)
(reduce #'fcompose-2 args))
Run Code Online (Sandbox Code Playgroud)
除了我为了定义而不小心写下了以下内容fcompose
.
(defun fcompose (&rest args)
(reduce #'fcompose args))
Run Code Online (Sandbox Code Playgroud)
自然导致堆栈溢出.