Storing objects in Session

Discussion in 'Perl Misc' started by prabhu.subramaniam@gmail.com, Nov 19, 2006.

  1. Guest

    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
     
    , Nov 19, 2006
    #1
    1. Advertising

  2. Prabhu Guest

    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

    On Nov 20, 1:16 am, Sherm Pendley <> wrote:
    > writes:
    > > I am trying to store an object (WWW::Mechanize) in
    > > session(CGI::Session) and trying to retrieve itWhy? WWW::Mechanize is for writing HTTP clients. CGI::Session is used on a

    > server to manage client sessions. They're completely unrelated.
    >
    > What are you actually trying to accomplish?
    >
    > sherm--
    >
    > --
    > Web Hosting by West Virginians, for West Virginians:http://wv-www.net
    > Cocoa programming in Perl:http://camelbones.sourceforge.net
     
    Prabhu, Nov 19, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sandra
    Replies:
    0
    Views:
    326
    Sandra
    Oct 21, 2003
  2. =?Utf-8?B?QW50aG9ueSBSZXlub2xkcw==?=

    Storing objects in Session

    =?Utf-8?B?QW50aG9ueSBSZXlub2xkcw==?=, Apr 6, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    493
    Joey Powell
    Apr 7, 2004
  3. yurps

    Storing objects in ASP.NET Session

    yurps, Jun 8, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    414
    yurps
    Jun 8, 2005
  4. MrShovel

    Storing Objects in Session Object

    MrShovel, Oct 30, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    7,137
    MrShovel
    Oct 31, 2005
  5. Mark Rae
    Replies:
    10
    Views:
    8,766
    Ray Booysen
    May 17, 2006
Loading...

Share This Page