Subject: | storing objects in Graph |
It would be nice to be able to add objects to the Graphs. I was using Graph::Directed and Graph::DFS to build a graph whose vertices were my own objects. This spelled trouble when I tried to do a DFS through my graph, and then used the retrieved objects, since they weren't all retrieved as objects.
I tracked the source of my trouble in Graph::Traversal::reset() which creates a pool keyed to the vertices, which are stringified in the process. I've attached my test file that I was using to help locate where things were going wrong.
I'm going to get around this by Data::Dumper::Dump'ing my object to a string, and then eval'ing after I get it back. I don't if it would be easy to have Graph::* support storing objects, but it would be nice.
Please et me know if I'm somehow laboring under some kind of misunderstanding.
//Ed
use Test::More ( no_plan );
use strict;
use Graph::Directed;
use Graph::DFS;
use Data::Dumper;
# create a graph
my $v1 = foo->new( name => 'v1' );
my $v2 = foo->new( name => 'v2' );
my $v3 = foo->new( name => 'v3' );
my $G = Graph::Directed->new();
$G->add_path( $v1, $v2, $v3 );
# verify that our objects are in the graph
foreach my $v ( $G->vertices() ) {
isa_ok( $v, 'foo', "vertices() knows $v " );
}
# now try to iterate through them with Graph::Traversal
my $dfs = Graph::DFS->new( $G );
while ( my $v = $dfs->next_postorder() ) {
isa_ok( $v, 'foo', "next_postorder() knows $v " );
}
# foo class
package foo;
sub new {
my ( $class, %opts ) = @_;
return( bless { %opts } );
}
sub name {
my $self = shift;
return( $self->{name} );
}