pavan
pavan

Reputation: 314

how can i store an object in perl session

I am having an Emp (class) object with fields name, id and password:

$emp = new Emp({name=>'pavan', id=>101, password=>'05cc11'});

$serializble = freeze($emp);

Storing in session:

$session->param("emp",$serializble);

But when I open the session object which is stored in the tmp directory, the emp value is undef.

Update

Updated after using Storable to serialize the object.

$serializble = freeze($emp);

$session->param("emp",$serializble);

this is the session file (with added line breaks for readability):

$D = {'_SESSION_ID' => 'dd75c6042893334a6bf26794b4ce5c74',
'_SESSION_ATIME' => 1356628765,
'emp' => '^D^G^D1234^D^D^D^H^Q^CPEmp^C^B^@^@^@ ^Epavan^D^@^@^@name^H�^B^@^@^@id',
'_SESSION_REMOTE_ADDR' => '127.0.0.1',
'_SESSION_CTIME' => 1356628765};;$D

When I try to return the object from the session it returns undef:

$recoverable = thaw($session->param('emp');

print $recoverable; 

here is my total code

Emp class:

package Emp;

sub new{
    my ($class, $args) = @_;

    my $self = bless{}, $class;

    $self->{name} = $args->{name};
    $self->{id}=$args->{id};

    return $self;
}

sub getEmpname{
    my $self = shift;
    return $self->{name};
}

1;

emp.cgi

$query = new CGI();

$session = new CGI::Session("driver:File", undef, {Directory => "/tmp"});

$emp = new Emp( { name => $query->param('username'),
                  id   => 101
                } );
my $serialized = freeze($emp);

$session->param("emp", $serialized);

$login = $emp->getEmpname();  #it is the method of Emp class

$cookie  = $query->cookie( -name    => $login,
                           -value   => $session->id,
                           -expires => '+3M',
                         );

print $query->header( -type    => 'text/html',
                      -cookie  => $cookie,
                    );

welcome.cgi

$q = new CGI();
   
$sid = $q->cookie('login') || $q->param('login') || undef;
  
$session = new CGI::Session(undef, $sid, {Directory=>'/tmp'});
  
print $q->header('text/html');
print $q->start_html('title');
print "<br>";
print Dumper $session->param('emp');
my $emp = thaw( $session->param('emp') );  //which is saved in session object.
print $emp->getEmpname();
print end_html;

Upvotes: 1

Views: 2001

Answers (1)

amon
amon

Reputation: 57640

You cannot simply store an object, where a string is expected.

If we have an object $o of class Emp, and assuming that this object is implemented using hashes, than the stringification of that object would be something like Emp=HASH(0x9bc8880). The information included in this stringification does not allow the restoration of the object from this string.

Instead, you have to choose a serialization of the object, so that you can restore the object.

The Data::Dumper module serializes data structures as Perl code, that can be evaled to recreate the original value. The Storable module stores the data in a binary format, and might be applicable here.

You can serialize a data structure (or object) via freeze, and restore it via thaw.

use strict; use warnings; use Storable qw(freeze thaw); use Data::Dumper;

my $o = bless {a => 1, b => 2}, 'Emp';

print "> Dumper representation of original ($o)\n";
print Dumper $o;
print "> serializing the object...\n";
my $serialized = freeze($o);
print "> restoring the object...\n";
my $restored = thaw($serialized);
print "> Dumper representation of copy ($restored)\n";
print Dumper $restored;

Output:

> Dumper representation of original (Emp=HASH(0x8de78c8))
$VAR1 = bless( {
                 'a' => 1,
                 'b' => 2
               }, 'Emp' );
> serializing the object...
> restoring the object...
> Dumper representation of copy (Emp=HASH(0x8f5df64))
$VAR1 = bless( {
                 'a' => 1,
                 'b' => 2
               }, 'Emp' );

or similiar. Note that the restored object has a different memory location, but is equivalent otherwise.

If you want to serialize a data structure to a file, you can use store and retrieve instead; consult the documentation for further examples.

Upvotes: 4

Related Questions