可以在进程之间传递 Perl 哈希引用吗?
我有一个 ActiveState PerlCtrl 项目。我想知道是否可以在 COM DLL 中包含哈希值,将其引用作为字符串(例如“HASH(0x2345)”)传递给调用进程,然后将该字符串传递回 COM DLL 并以某种方式祝福它重新指向相关的哈希值。
使用 return "" 获取 hashref 似乎很容易。 \%Graph;
我尝试过类似 $Graph = shift; 的方法$Graph = bless {%$Graph};
但他们似乎没有实现我所追求的目标。 %Graph 哈希至少对于模块来说是全局的。
测试代码(VBScript):
set o = CreateObject("Project.BOGLE.1")
x = o.new_graph()
wscript.echo x
x = o.add_vertex(x, "foo")
I have an ActiveState PerlCtrl project. I'd like to know if it's possible to have a hash in the COM DLL, pass it's ref out to the calling process as a string (e.g. "HASH(0x2345)") and then pass that string back into the COM DLL and somehow bless it back into pointing to the relevant hash.
Getting the hashref seems easy enough, using return "" . \%Graph;
and I have tried things like $Graph = shift; $Graph = bless {%$Graph};
but they don't seem to achieve what I'm after. The %Graph hash is at least global to the module.
The testing code (VBScript):
set o = CreateObject("Project.BOGLE.1")
x = o.new_graph()
wscript.echo x
x = o.add_vertex(x, "foo")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这些是不同的进程,您将需要序列化哈希的内容或将其持久存储在磁盘文件中。要执行前者,请参阅 Storable 或 Data::Dumper;对于后者,这取决于它是简单标量的散列还是更复杂的东西。
如果是同一个进程中的同一个 perl 解释器,您可以保留一些全局变量,例如 %main::hashes;
在将字符串化引用传递回调用进程之前设置
$main::hashes{\%Graph} = \%Graph
,然后使用它来查找实际的哈希引用。但不要这样做:http://perlmonks.org/?node_id=379395。
If these are different processes, you will need to either serialize the content of the hash or persistently store it in a disk file. To do the former, see Storable or Data::Dumper; for the latter, it depends whether it's a hash of simple scalars or something more complex.
If it is the same perl interpreter in the same process, you can keep some global variable like %main::hashes;
set
$main::hashes{\%Graph} = \%Graph
before passing the stringified reference back to the calling process, then later use it to look up the actual hash reference.Don't do this, though: http://perlmonks.org/?node_id=379395.
不,您无法在进程之间可靠地传递哈希引用。
No, you can't reliably pass hash references between processes.