Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Apache::Session help storing a complex data structure

by B-A-T (Novice)
on Jan 05, 2009 at 03:19 UTC ( [id://734099]=perlquestion: print w/replies, xml ) Need Help??

B-A-T has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to store a data structure in using Apache::Session::MySQL.
The session creation and retrieval works fine for scalar values,HoH or Array etc,
but the session is not written if i try to store a tree data structure (N-ary tree http://search.cpan.org/~rkinyon/Tree-1.01/lib/Tree.pm) The code is as follows: --
my %session; tie %session,'Apache::Session::MySQL', undef, { DataSource => 'dbi:mysql:sessions', UserName => 'login', Password => 'pass', LockDataSource => 'dbi:mysql:sessions', LockUserName => 'login', LockPassword => 'pass' }; my $id = $session{_session_id}; $session{TIMESTAMP} =time; $session{tree} = $treeP; ## $treeP is a N-ary Tree $session{uname} = $uname; untie(%session) || die " Cant untie! \n"; ##-- in another code my %session1; tie %session1,'Apache::Session::MySQL', undef, { DataSource => 'dbi:mysql:sessions', UserName => 'login', Password => 'pass', LockDataSource => 'dbi:mysql:sessions', LockUserName => 'login', LockPassword => 'pass' }; my $u = $session1{uname}; ## this value is undef if I try to store the + tree as well in the above code print STDERR "name $u \n"; untie(%session1) or die "CANT CLOSE!! \n";
========

Can anyone help?

Thanks in advance...

cheers!

Replies are listed 'Best First'.
Re: Apache::Session help storing a complex data structure
by ikegami (Patriarch) on Jan 05, 2009 at 04:25 UTC
    The docs for Apache::Session say that Storable (and more specifically nfreeze and thaw) are used to serialize the data. But when I try to freeze a Tree, I get a fatal error.
    use strict; use warnings; use Tree qw( ); use Storable qw( nfreeze thaw ); use Data::Dumper qw( Dumper ); my $tree = Tree->new( 'root' ); my $child = Tree->new( 'child' ); $tree->add_child( $child ); print(Dumper(thaw(nfreeze($tree))));
    Can't store CODE items at ..\..\lib\Storable.pm (autosplit into ..\..\ +lib\auto\Storable\_freeze.al) line 339, at script.pl line 12

    Are you getting any errors? I haven't tried actually using Apache::Session. Can that be used from a command line script?

      Thankyou for replying,and Yes, the Command line script works. But, you are correct - the problem is with serializing the Tree, and not Apache::Session. I couldnt catch it myself.


      So, the only option with me is: creating/using another data structure?

      It would have been immense help, if Tree could work.

      Thanks!
Re: Apache::Session help storing a complex data structure
by CountZero (Bishop) on Jan 05, 2009 at 06:23 UTC
    Did you try other serializers?

    Apache::Session::Flex allows you to choose other serializers (such as Apache::Session::Serialize::YAML) that may perhaps be able to work with trees.

    Update: Using ikegami's script but replacing Storable with YAML seems to work:

    use strict; use warnings; use Tree qw( ); use YAML qw( Dump Load ); use Data::Dumper qw( Dumper ); my $tree = Tree->new( 'root' ); my $child = Tree->new( 'child' ); $tree->add_child( $child ); print(Dumper(Load(Dump($tree))));
    Output:
    $VAR1 = bless( { '_parent' => \undef, '_value' => 'root', '_children' => [ bless( { '_parent' => $VAR1, '_value' => 'child', '_children' => [], '_meta' => {}, '_depth' => '1', '_error_handler' => sub { " +DUMMY" }, '_width' => '1', '_handlers' => { 'value' => + [], 'add_child +' => [], 'remove_ch +ild' => [] }, '_root' => $VAR1, '_height' => '1', '_last_error' => undef }, 'Tree' ) ], '_meta' => {}, '_depth' => '0', '_error_handler' => $VAR1->{'_children'}[0]{'_error_h +andler'}, '_width' => '1', '_handlers' => { 'value' => [], 'add_child' => [], 'remove_child' => [] }, '_root' => $VAR1, '_height' => '2', '_last_error' => undef }, 'Tree' );

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      no it dint work ! :( But a little improvement the other variables(name, timestamp) are stored normally, only the tree is not being written. :|

      ..
Re: Apache::Session help storing a complex data structure
by jasonk (Parson) on Jan 06, 2009 at 20:57 UTC

    You don't indicate what the error messages are, but Tree objects include code references. In order to serialize them with Storable, you either need to have a recent version of Storable (after 2.05) and set $Storable::Deparse to a true value, to allow Storable to serialize the code reference, or prevent the code references from being serialized (see STORABLE_freeze and STORABLE_thaw in Storable).


    www.jasonkohles.com
    We're not surrounded, we're in a target-rich environment!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found