Note that there are some explanatory texts on larger screens.

plurals
  1. POhow can i store an object in perl session
    primarykey
    data
    text
    <p>I am having an Emp (class) object with fields <code>name</code>, <code>id</code> and <code>password</code>:</p> <pre><code>$emp = new Emp({name=&gt;'pavan', id=&gt;101, password=&gt;'05cc11'}); $serializble = freeze($emp); </code></pre> <p>Storing in session:</p> <pre><code>$session-&gt;param("emp",$serializble); </code></pre> <p>But when I open the session object which is stored in the tmp directory, the emp value is <code>undef</code>. </p> <h3>Update</h3> <p>Updated after using Storable to serialize the object.</p> <pre><code>$serializble = freeze($emp); $session-&gt;param("emp",$serializble); </code></pre> <p>this is the session file (with added line breaks for readability):</p> <pre><code>$D = {'_SESSION_ID' =&gt; 'dd75c6042893334a6bf26794b4ce5c74', '_SESSION_ATIME' =&gt; 1356628765, 'emp' =&gt; '^D^G^D1234^D^D^D^H^Q^CPEmp^C^B^@^@^@ ^Epavan^D^@^@^@name^H�^B^@^@^@id', '_SESSION_REMOTE_ADDR' =&gt; '127.0.0.1', '_SESSION_CTIME' =&gt; 1356628765};;$D </code></pre> <p>When I try to return the object from the session it returns <code>undef</code>:</p> <pre><code>$recoverable = thaw($session-&gt;param('emp'); print $recoverable; </code></pre> <p>here is my total code</p> <p><strong>Emp class:</strong></p> <pre><code>package Emp; sub new{ my ($class, $args) = @_; my $self = bless{}, $class; $self-&gt;{name} = $args-&gt;{name}; $self-&gt;{id}=$args-&gt;{id}; return $self; } sub getEmpname{ my $self = shift; return $self-&gt;{name}; } 1; </code></pre> <p><strong>emp.cgi</strong></p> <pre><code>$query = new CGI(); $session = new CGI::Session("driver:File", undef, {Directory =&gt; "/tmp"}); $emp = new Emp( { name =&gt; $query-&gt;param('username'), id =&gt; 101 } ); my $serialized = freeze($emp); $session-&gt;param("emp", $serialized); $login = $emp-&gt;getEmpname(); #it is the method of Emp class $cookie = $query-&gt;cookie( -name =&gt; $login, -value =&gt; $session-&gt;id, -expires =&gt; '+3M', ); print $query-&gt;header( -type =&gt; 'text/html', -cookie =&gt; $cookie, ); </code></pre> <p><strong>welcome.cgi</strong></p> <pre><code>$q = new CGI(); $sid = $q-&gt;cookie('login') || $q-&gt;param('login') || undef; $session = new CGI::Session(undef, $sid, {Directory=&gt;'/tmp'}); print $q-&gt;header('text/html'); print $q-&gt;start_html('title'); print "&lt;br&gt;"; print Dumper $session-&gt;param('emp'); my $emp = thaw( $session-&gt;param('emp') ); //which is saved in session object. print $emp-&gt;getEmpname(); print end_html; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload