Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

CGI::Sessions fails when I use Data::Dumper

by nodebunny (Novice)
on Sep 20, 2011 at 14:24 UTC ( [id://926954]=perlquestion: print w/replies, xml ) Need Help??

nodebunny has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: CGI::Sessions fails when I use Data::Dumper
by moritz (Cardinal) on Sep 20, 2011 at 14:36 UTC
      Thanks I promise to do better next time
Re: CGI::Sessions fails when I use Data::Dumper
by Anonymous Monk on Sep 20, 2011 at 14:38 UTC
      Thank you that last link solved my problem exactly --
      $cgi->escapeHTML( Dumper( $cgi, $sss) );

        escapeHTML oddly only takes 1 argument, and ignores the rest, so that only works if you have

        sub Dumper { scalar Data::Dumper->new( \@_ )->Indent(1)->Useqq(1)->Dum +p; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://926954]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-16 19:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found