请注意以下最小的工作示例:
use warnings;
use strict;
use IPC::Shareable;
use Data::Printer;
IPC::Shareable->clean_up;
my $sharevar1 = "a";
my $sharevar2;
print "A: $sharevar1 $sharevar2\n";
p($sharevar1);
p($sharevar2);
my $glue1 = 'glu1';
my $glue2 = 'glu2';
my %options = (
create => 1, #'yes',
exclusive => 0,
mode => 0644, #0644,
destroy => 1, # 'yes',
);
my $sharevar_handle1 = tie $sharevar1, 'IPC::Shareable', $glue1 , \%options ; #
print "B1: $sharevar1 $sharevar2 - $sharevar_handle1\n";
my $sharevar_handle2 = tie $sharevar2, 'IPC::Shareable', $glue2 , \%options ; #
print "B2: $sharevar1 $sharevar2 - $sharevar_handle2\n";
p($sharevar1);
p($sharevar2);
$sharevar1 = "b";
#~ $sharevar1 = "AOE" . \$sharevar2;
$sharevar2 = 20;
print "C: ";
print "- $sharevar1 $sharevar2\n";
p($sharevar1);
p($sharevar2);
当我运行它时,我得到如下输出 - 正如预期的那样:
Use of uninitialized value $sharevar2 in concatenation (.) or string at tt.pl line 13.
A: a
"a"
undef
Use of uninitialized value $sharevar1 in concatenation (.) or string at tt.pl line 30.
Use of uninitialized value $sharevar2 in concatenation (.) or string at tt.pl line 30.
B1: - IPC::Shareable=HASH(0xa1dc1b8)
Use of uninitialized value $sharevar1 in concatenation (.) or string at tt.pl line 34.
Use of uninitialized value $sharevar2 in concatenation (.) or string at tt.pl line 34.
B2: - IPC::Shareable=HASH(0xa215b10)
undef (tied to IPC::Shareable)
undef (tied to IPC::Shareable)
C: - b 20
"b" (tied to IPC::Shareable)
20 (tied to IPC::Shareable)
但是,如果现在我尝试取消注释“ $sharevar1 = "AOE" . \$sharevar2;
”行,同时注释掉$sharevar1 = "b";
上面的“”;那么我得到的输出几乎是相同的,除了最后:
...
B2: - IPC::Shareable=HASH(0x852fb20)
undef (tied to IPC::Shareable)
undef (tied to IPC::Shareable)
Can't use string ("AOESCALAR(0x836bf88)") as a SCALAR ref while "strict refs" in use at /usr/local/share/perl/5.10.1/IPC/Shareable.pm line 741.
C: $
现在,问题是这个“不能使用字符串......”实际上会导致崩溃......显然,如果一个tie
d 变量曾经被分配一个引用 via \
,它会得到一个类似于SCALAR(0x836bf88)
string的值,这显然被解释为Perl 之后表示地址...?!
而且我认为 Perl 在这种情况下会匹配SCALAR(...
字符串的起始部分 (对于“带括号的 0x”有点匹配):“不能使用字符串 ("AOESCALAR(0x836bf88)") 作为 SCALAR ref" ...
我的问题是 - 我对 Perl 和 IPC::Shareable 如何正确解释地址(否则存储为“字符串”)的推理是正确的(不,请参阅下面的编辑;是的,请参阅帖子);不管是不是,我将如何将地址存储到 IPC::Shareable 中?
非常感谢您的任何答案,
干杯!
编辑:嗯,显然,打印包含字符串地址的字符串变量通常都可以正常工作 - 所以这个问题是 IPC::Shareable 特定的,我猜:
DB<1> $ttt = "aa"
DB<2> p $ttt
aa
DB<3> $eee = \$ttt
DB<4> p $eee
SCALAR(0xa382668)
DB<5> $eee = "erw".\$ttt
DB<6> p $eee
erwSCALAR(0xa382668)
DB<7> q