I am storing a hash in session. I am trying to retrieve the values from session, but is not successful. I want to loop through the hash value in session to generate a select box. Below is the session value
$VAR1 = {
'userDetails' => {
'roles' => [
{
'ln' => 'asdf',
'email' => '[email protected]',
'session_id' => '14',
'is_active' => '0',
'role' => 'ndfbfd',
'facility_name' => 'jjjj',
'fn' => 'yyyyyy'
},
{
'ln' => 'asdf',
'email' => '[email protected]',
'session_id' => '15',
'is_active' => '1',
'role' => 'ndfbfd',
'facility_name' => 'fbhsdf',
'fn' => 'yyyyyy'
},
{
'ln' => 'asdf',
'email' => '[email protected]',
'session_id' => '16',
'is_active' => '1',
'role' => 'ndfbfd',
'facility_name' => 'mvsd',
'fn' => 'yyyyyy'
},
{
'ln' => 'asdf',
'email' => '[email protected]',
'session_id' => '17',
'is_active' => '1',
'role' => 'bdfgre',
'facility_name' => 'jjjj',
'fn' => 'yyyyyy'
},
{
'ln' => 'asdf',
'email' => '[email protected]',
'session_id' => '18',
'is_active' => '0',
'role' => 'gderere',
'facility_name' => 'jjjj',
'fn' => 'yyyyyy'
}
],
'ln' => 'asdf',
'logged_in' => '1',
'fn' => 'yyyyyy'
},
'logged_in' => '1',
'username' => '[email protected]'
};
I am trying to retrieve the roles from the session
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";
}
It is showing two errors.
Reference found where even-sized list expected.
Odd number of elements in hash assignment.
When I change the code my %userDetails = $self->session('userDetails');
to my %userDetails = \$self->session('userDetails');
I am getting the error
Odd number of elements in hash assignment.
Odd number of elements in hash assignment.
Just guessing blatantly
my %userDetails = %{$self->session('userDetails')};
my @roles = @{$userDetails{'roles'}};
require Data::Dumper;
foreach my $family ( @roles ) {
print Data::Dumper::Dumper($family);
}
In your example
'roles' => [
so this indicates that the value beneath the role attribute is an array reference.
$self->session('userDetails')
might be hash, or more probably a hash-reference. I guessed the later. So if you have a reference to an "element" you derefence it first. See perldoc perlref