http://qs321.pair.com?node_id=329088

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

I've got a set of modules that use objects to represent nodes on a graph, and uses simple references to represent the arcs between them. What I'm trying to do now is introduce a concept whereby I can declare an arc as 'active' or 'inactive' without severing the arc. Similar to how you can leave a lamp plugged into the wall socket, and the electricity is either on or off.

What I'm wondering is if there might be a good metaphor using simple perl syntax to represent this phenomenon without a secondary data structure to store this active/inactive information. I tried adding a level of indirection, such as:
#active: push @{$object->{related}}, $object2; #inactive @{$object->{related}}, \$object2;
but that got to be a nightmare in remembering to dereference it everywhere. So I'm now wondering if there's a way to tag the reference in such a way as to be able to test for it's 'activeness'. Scalar::Util's weaken() function would be a great metaphor for this (testing with the isweak() function) but I can't afford to have weaken's intended effect as a side effect.

Note that I don't want to annotate the targeted object. The object isn't 'active' or 'inactive'. It's the arc drawn between the two.

I'm willing to entertain black magic at this point even if it doesn't prove feasible down the line. I'm a stickler for useable, straightforward syntax, even if it means some vodoo underneath.

update: After some discussion in the chatterbox with Mr. Muskrat and atcroft it became obvious that I hadn't provided enough information: At the moment the best strategy seems to be to keep the active/inactive information in a parallel data structure inside the tied array. That way i can do something like
[% IF object.related.isActive( related_obj ) %]...[% END %]
but if anyone has any other interesting approaches to this I'd be glad to hear them. The simpler for the template authors, the better.

Replies are listed 'Best First'.
Re: adding meta-data to references-as-graph-arcs
by chromatic (Archbishop) on Feb 15, 2004 at 07:02 UTC

    I'm half tempted to suggest using dualvar() (from Scalar::Util, I believe), overriding the boolean comparator in your class to use that value.

    It's worth tons of cool points if it works, but I wouldn't really do it that way.

Re: adding meta-data to references-as-graph-arcs
by kvale (Monsignor) on Feb 15, 2004 at 06:24 UTC
    It seems to me that dealing with references and and parallel decorations of references seems pretty complex for folk not expert in these things. A simpler approach may be to abstract away the graph representation and algorithms from the objects themselves and create a graph object proper with objects associated with the nodes.

    This can probably be done most easily by using Graph::Directed to handle graph stuff and adding node number to the object's data to be set upon initialization. The module has all the (basic) graph methods you could want, with no worries about dereferencing, just simple method calls.

    -Mark