如何从Perl中的会话中检索哈希值

时间:2018-02-23 09:19:34

标签: perl mojolicious strawberry-perl

我在会话中存储哈希。我试图从会话中检索值,但不成功。我想循环遍历会话中的哈希值以生成一个选择框。以下是会话值

$VAR1 = {
          'userDetails' => {
                           'roles' => [
                                      {
                                        'ln' => 'asdf',
                                        'email' => 'test@example.com',
                                        'session_id' => '14',
                                        'is_active' => '0',
                                        'role' => 'ndfbfd',
                                        'facility_name' => 'jjjj',
                                        'fn' => 'yyyyyy'
                                      },
                                      {
                                        'ln' => 'asdf',
                                        'email' => 'test@example.com',
                                        'session_id' => '15',
                                        'is_active' => '1',
                                        'role' => 'ndfbfd',
                                        'facility_name' => 'fbhsdf',
                                        'fn' => 'yyyyyy'
                                      },
                                      {
                                        'ln' => 'asdf',
                                        'email' => 'test@example.com',
                                        'session_id' => '16',
                                        'is_active' => '1',
                                        'role' => 'ndfbfd',
                                        'facility_name' => 'mvsd',
                                        'fn' => 'yyyyyy'
                                      },
                                      {
                                        'ln' => 'asdf',
                                        'email' => 'test@example.com',
                                        'session_id' => '17',
                                        'is_active' => '1',
                                        'role' => 'bdfgre',
                                        'facility_name' => 'jjjj',
                                        'fn' => 'yyyyyy'
                                      },
                                      {
                                        'ln' => 'asdf',
                                        'email' => 'test@example.com',
                                        'session_id' => '18',
                                        'is_active' => '0',
                                        'role' => 'gderere',
                                        'facility_name' => 'jjjj',
                                        'fn' => 'yyyyyy'
                                      }
                                    ],
                           'ln' => 'asdf',
                           'logged_in' => '1',
                           'fn' => 'yyyyyy'
                         },
          'logged_in' => '1',
          'username' => 'test@example.com'
        };

我正在尝试从会话中检索角色

my %userDetails = $self->session('userDetails');
my %roles = $userDetails{'roles'};
foreach my $family ( keys %roles ) {
    print "$family: { ";
    for my $role ( keys %{ $HoH{$family} } ) {
        print "$role=$HoH{$family}{$role} ";
    }
    print "}\n";
}

显示两个错误。

Reference found where even-sized list expected.
Odd number of elements in hash assignment.

当我将代码my %userDetails = $self->session('userDetails');更改为my %userDetails = \$self->session('userDetails');时,我收到了错误

Odd number of elements in hash assignment.
Odd number of elements in hash assignment.

1 个答案:

答案 0 :(得分:0)

公然猜测

my %userDetails = %{$self->session('userDetails')};
my @roles = @{$userDetails{'roles'}};
require Data::Dumper;
foreach my $family ( @roles ) {
    print Data::Dumper::Dumper($family);
}

在你的例子中

  

'roles'=> [

因此,这表示role属性下面的值是数组引用。

  

$自>会话( '为userDetails')

可能是哈希,或者更可能是哈希引用。我猜到了后来。因此,如果你有一个“元素”的引用,你首先要解除它。见perldoc perlref