Storing objects in Session

P

prabhu.subramaniam

Hi,
I am trying to store an object (WWW::Mechanize) in
session(CGI::Session) and trying to retrieve it, but I am encountering
the following problem:

Prog:

my $mech = WWW::Mechanize->new ( );

if(request is from pagel)
{
$session = new CGI::Session("driver:File", undef,
{Directory=>'/tmp'});
$mech->get("www.someurl.com"); #works fine
$session->param('Mech', $mech);
}
elseif(request is from page2)
{
$sid = param('CGISESSID') || undef;
$session = new CGI::Session("driver:File", $sid,
{Directory=>'/tmp'}); #able to get other data passed from page1
$mech = $session->param('Mech');
$mech->get("www.someotherurl.com");
}

When I am in first page things are fine. In second page, I get
"Can't locate object method "get" via package "WWW::Mechanize" at
C:\Inetpub\Scripts\somescript.pl line 100."

I am a beginner to Perl and I am not able to find any resources to
solve my problem. It is important that I reuse the same object($mech
from page1's call). Your help on this is greatly appreciated. Thanks.

Regards,
Prabhu
 
P

Prabhu

Hi,
@Sherem - Sorry I am new to this forum. I am not familiar with the
posting guidelines. Anyway, my question is on how to store objects into
a session retrieve it back. The code I pasted qas more of a pseudocode
than the real program. What I am trying to do is to contact a website,
authenticate myself and request a file whose name will be entered by
the user upon successful login. So, it requires 2 inputs and hence the
if..else part. I figured I need to use the same www::Mechanize object
for both the requests. I am trying to store this $mech object in
session and use it again when the request comes back with the name of
the file needed. Any suggestions? Thanks.

use strict;
use WWW::Mechanize;
use HTML::TokeParser;
use HTTP::Cookies;
use IO::File;
use CGI qw:)standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use CGI::Session;
use CGI;

my $email = "";

my $mech = WWW::Mechanize->new ( cookie_jar => {} );

if(param('FormName') eq 'Auth')
{
my $session = new CGI::Session("driver:File", undef,
{Directory=>'/tmp'});
my $sid = $session->id();

$email = param('Email');

$session->param('Email', $email);

#something I tried, it works
#$session->param('Mech', $mech);
#$mech = "";
#$mech = $session->param('Mech');

$mech->agent_alias('Windows IE 6');
$mech->get("www.somesite.com");
#do operations here
$session->param('Mech', $mech);

print 'Status: 302 Moved', "\r\n", "Location:
http://localhost/testsite/Confirmation.html?id=someid&CGISESSID=$sid","\r\n\r\n";

}
else
{
my $sid = param('CGISESSID') || undef;
my $session = new CGI::Session("driver:File", $sid,
{Directory=>'/tmp'});

$email = $session->param('Email'); #works

$mech = $session->param('Mech'); #at this point error : Can't locate
object method "get" via package "WWW::Mechanize" at
C:\Inetpub\Scripts\somescript.pl line 100.

$mech->get("www.somesite.com");
#do operations here
}

Regards,
Prabhu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top