Hi monks. I'm a noob to perl and I've only begun learning it this past month. I've run into all kinds of problems using CGI::Session, in particular trying to use Dumper to see whats going on as things change, and outputting html. In your wisdom what is a quick and easy method for managing a Session object in perl? (or alternatively what is wrong with my code?)

I've read and done the tutorials got CGI::Session -- but when I print html to stdout or use Data::Dumper to show whats in my session object, Perl gets mad at me!
Updated with code

**My::Role::PersistantData**
package My::Role::PersistsData; use Moose::Role; use namespace::autoclean; has '_cgi' => ( is => 'rw', isa => 'Maybe[CGI]', builder => '_build_cgi' ); has '_sss' => ( is => 'rw', isa => 'Maybe[CGI::Session]', builder => '_build_sss' );


**My::Session**
package My::Session; use Moose; use namespace::autoclean; with 'My::Role::PersistsData'; use CGI; use CGI::Session ('-ip_match'); use CGI::Carp qw/fatalsToBrowser warningsToBrowser/; sub start{ my($self) = @_; my $cgi = $self->cgi(); $self->log("Session Started!"); } sub cgi{ my($self) = @_; $self->_cgi = $self->_build_cgi() unless $self->_cgi; return ($self->_cgi); } sub _build_cgi{ my($self) = @_; my $cgi = CGI->new(); if(!$cgi){ #print "mising cgi"; } return ( $cgi ); } sub _build_sss{ my($self) = @_; my $cgi = $self->cgi(); my $sid = $cgi->cookie("CGISESSID") || $cgi->param('CGIS +ESSID') || undef; $self->log("Session ID Initial is: ".($sid?$sid:"undef") +); my $sss = CGI::Session->new(undef, $cgi, {Directory=>'t +mp'}) or die CGI::Session->errstr; my $cookie = $cgi->cookie(CGISESSID => $sss->id() ); $self->log("Resulting Session ID is: ".$sid." cookie is: + ".$cookie); print $cgi->header( -cookie=>$cookie ); return ( $sss ); }


**main.pl**
use Data::Dumper; $Data::Dumper::Sortkeys = 1; use CGI; use CGI::Carp qw(fatalsToBrowser); use My::Session; $| = 1; $, = " "; $\ = "\n <br />"; my $sss = My::Session->new(); $sss->start(); print "<title>Test Sessions</title>"; print "TEST"; print "<pre>",Dumper($sss),"</pre>";


It's pretty weird because the first time I run this I get an actual CGISESSION ID and I am able to carry it over on a page refresh...
however if I load the page again, suddenly the $sss (session) comes back as undefined, when it should return a new Session object:
$sss = new CGI::Session("driver:File", $sid, {Directory=>'/tmp'})

A few tweaks to my code revealed this error:
new(): failed: load(): couldn't thaw() data using CGI::Session::Se +rialize::default:thaw(): couldn't thaw. syntax error at (eval 253) li +ne 2, near &quot;/&gt;&quot;

Looks like something relating to an html tag!

I also snooped around in CGI::Session.pm and found where this error was being thrown at, I guess it's not able to parse _DATA ...because of these html entities... "/>" **CGI::Session.pm**
.... $self->{_DATA} = $self->{_OBJECTS}->{serializer}->thaw($raw_da +ta); unless ( defined $self->{_DATA} ) { #die $raw_data . "\n"; return $self->set_error( "load(): couldn't thaw() data usi +ng $self->{_OBJECTS}->{serializer} :" . $self->{_OBJECTS}->{serializer}->e +rrstr ); }
Low and behold removing Data::Dumper and all my html output fixed this -- I think -- but that doesnt necessarily solve my problem or help me understand why this is breaking

In reply to CGI::Sessions fails when I use Data::Dumper by nodebunny

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.