Subject: | Bug (and fix) for GraphViz::Data::Grapher (v2.02) |
Dear Leon,
(Hi, I met you a few years back when you organised that Perl Conference
at the ICA and I gave a paper).
I've found and fixed a bug in GraphViz::Data::Grapher whereby
a data structure such as:
my $data = {
'wibble[1]' => "hello"
};
does not graph properly - the "hello" is replaced with undef.
This is universal for all field names containing [], probably
other weird characters too.
The bug is caused by the chunk of code starting at line 115 of Grapher.pm:
my $hash = $graph->add_node({ label => \@hash, color => 'brown' });
foreach my $port (0.. @hash-1) {
my $key = $hash[$port];
my $target = _init($graph, $item->{$key});
}
as you see, add_node is called with label => \@hash. The problem is
that add_node ***MODIFIES*** array elements from the label (if it's an
array ref) by "escaping" weird characters.
Hence after add_node has modified @hash, the subsequent code extracts
a key from each (potentially changed) $hash[$port] and then retrieves
$item->{$key} - which retrieves undef cos $key has not got the original
value but an "escaped" version!
The fix is of course simple - copy @hash into another array (@labels)
and pass the copy into add_node for twiddling:
my @labels = @hash;
my $hash = $graph->add_node({ label => \@labels, color => 'brown' });
After this fix, the data structure is visualised correctly. Time
for GraphViz 2.03 perchance?
cheers
Duncan White
Duncan C. White, CSG Deputy Manager, Dept of Computing,
Imperial College, London, SW7 2BZ, UK.
Email: dcw@doc.ic.ac.uk Phone: 0207 594 8254
URL: http://www.doc.ic.ac.uk/~dcw/ Fax: 0207 594 8389
------------------------------------------------------------------------------
ALL THE REAVERS IN THE 'VERSE: GRAAAAAAAAAAAAAAAAAAAAAR!
- Serenity in 2000 words or less, Ron Swartzendruber
------------------------------------------------------------------------------
--- /homes/dcw/ORIGGrapher.pm 2005-11-24 13:13:53.181007000 +0000
+++ /usr/lib/perl5/vendor_perl/5.8.6/GraphViz/Data/Grapher.pm 2005-11-24 13:21:35.000000000 +0000
@@ -1,5 +1,7 @@
package GraphViz::Data::Grapher;
+# DCW: modified the @hash code cos add_node corrupts labels.
+
use strict;
use vars qw($VERSION);
use Carp;
@@ -112,7 +114,17 @@
foreach my $key (sort keys(%$item)) {
push @hash, $key;
}
- my $hash = $graph->add_node({ label => \@hash, color => 'brown' });
+ # DCW 24/11/2005: before this fix values of hash fields whose names
+ # contain [] characters were reported as undef, because
+ # add_node modifies it's label parameter to "escape"
+ # weird characters like [], hence after add_node the
+ # $item->{$key} lookups were failing cos @hash values
+ # were now "escaped". Here we fix the problem by
+ # duplicating @hash to @labels and giving add_node
+ # @labels (previously @hash) so that add_node's
+ # changes to @labels don't break our @hash lookups.
+ my @labels = @hash;
+ my $hash = $graph->add_node({ label => \@labels, color => 'brown' });
foreach my $port (0.. @hash-1) {
my $key = $hash[$port];
my $target = _init($graph, $item->{$key});