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

How to retrieve hash value from session in Perl

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

$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.

瞎猜的

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

在你的例子中

'roles' => [

所以这表明角色属性下的值是一个数组引用。

$self->session('userDetails')

可能是散列,或者更可能是 hash-reference。我猜是后者。所以如果你有一个 "element" 的引用,你首先要取消引用它。参见 perldoc perlref