所以我觉得这是我的一些愚蠢的错误,但我无法弄清楚发生了什么。
首先是上下文:我正在玩webRTC,使用perl和js构建一个小型视频应用程序。我害怕被困在webRTC部分,但事实证明我被perl困住了。
在我的文件的顶部,我实例化像这样的哈希
helper clients => sub { state $clients = {} }
每当有人开始使用/
时,我会检查当前客户端是否有UID。如果不是我生成并存储它。然后我检查客户端是否已经在客户端哈希。
if (!$self->session('uid')) {
$self->session('uid' => sha256_hex($self->tx . irand()));
}
if (!exists $self->clients->{$self->session('uid')}) {
$self->clients->{$self->session('uid')} = $self->tx;
}
问题出现在我的信令服务器中。当我使用来自的价值 解码的json我得到一个未定义的错误。请注意循环是无用的,我是为了调试而做的。
这是相关代码:
$self->on(
message => sub {
my ( $ws, $message ) = @_;
my $json = decode_json($message);
for (keys %{$self->clients}) {
app->log->info('$_ is a => ' . $_);
# $_ => 0d96bc9bb05feb9a7c2889209ee777affcaa11252be54ca7ddc349889978850f
app->log->info('json uid => ' . $json->{'uid'});
# json uid => 0d96bc9bb05feb9a7c2889209ee777affcaa11252be54ca7ddc349889978850f
app->log->info('remote address with $_ => ' . $self->clients->{$_}->remote_address);
# remote address with $_ => 127.0.0.1
app->log->info('remote address with string => ' . $self->clients->{'0d96bc9bb05feb9a7c2889209ee777affcaa11252be54ca7ddc349889978850f'}->remote_address);
# remote address with string => 127.0.0.1
app->log->info('remote address with json_ => ' . $self->clients->{$json->{'uid'}}->remote_address);
# Can't call method "remote_address" on an undefined value at blabla
}
....
}
);
我不理解的是$_
和$json->{'uid'}
保持相同的值,那么为什么第一个工作而不是第二个?
答案 0 :(得分:3)
他们显然没有相同的价值观。尾随空白?
use Data::Dumper qw( Dumper );
{
local $Data::Dumper::Useqq = 1;
print(Dumper($json->{uid}));
}
或者在您的情况下,app->log->info
代替print
。