> I'm not sure how it can be done generically

Well, this is what I have for dumping a linked list where
the element is an array. [0] is payload. [1] is next. [2] is prev:


sub DumpLLArray{
        my($element, $next,$prev)=@_;

        $next=1 unless(defined($next));
        $prev=2 unless(defined($prev));

        use Data::Dumper;
        use Storable 'dclone';

        while($element){
                print "element is $element :\n";

                my @copy;

                # make a deep copy of everything but next/prev
                for(my $i=0; $i<scalar(@$element); $i++){
                        my $deepcopy;
                        if(($i == $next) or ($i == $prev)){
                                if(defined($element->[$i])){
                                        $deepcopy=($element->[$i]).'';
                                } else {
                                        $deepcopy='undef';
                                }
                        } else {
                                        my $dclone=dclone ([$element->[$i]]);
                                        $deepcopy=$dclone->[0];
                        }

                        push(@copy,$deepcopy);

                }

                # print out this element
                print Dumper \@copy;

                print "\n";
                $element=$element->[$next];
        }

}



The idea is for every element in the list, make a deep copy of
the element except for next/prev. Then you can call Dumper on
the copy of that element.

I think the above code is fairly generic that it should print
out a linked list regardless of what the actual payload is.

One could make a similar version for hashes.

As long as nothing in an element references any other element
it should be OK.   If an element might point to another element,
then you'd probably have to replace dclone with something that
will dclone everything BUT another element.

Should be able to detect an element by calling ref() on everything
as you manually dclone the data down. If ref() is the same as ref()
of the original element, then snip it and replace it with a stringification.

Greg


Greg




> I'm not sure how it can be done generically because there is no generic
> linked list structure in perl, right?  There is an array, though, so
> convert to an array and then use data dumper.  if the array is in the
> reverse order, Data::Dumper will indicate it's already printed the "next"
> portion and it'll look pretty.
>
> $ perl -lwe 'sub lldumper {
>   my ($node) = @_;
>   my @list;
>   while ($node) {
>     die q(usage: lldumper($hashref) where $hashref->next is another hash
> ref) unless ref $node eq "HASH";
>     unshift @list,$node;
>     $node = $node->{next};
>   }
>   return Dumper (@list);
> }
> use Data::Dumper;
> print lldumper({1=>2,next=>{"a"=>"b"}});'
> $VAR1 = {
>           'a' => 'b'
>         };
> $VAR2 = {
>           'next' => $VAR1,
>           '1' => 2
>         };
>
> Duane
>
> On Feb 8, 2013, at 10:48 AM, Greg London <[email protected]> wrote:
>
>>
>>
>> Is there a module out there for dumping linked lists in a legible
>> manner?
>>
>> I like that Data::Dumper gives you an output that can be "eval"ed back
>> in,
>> but when you give it a linked list, it gives you an output that's
>> totally
>> unreadable.
>>
>>
>>
>>
>>
>> _______________________________________________
>> Boston-pm mailing list
>> [email protected]
>> http://mail.pm.org/mailman/listinfo/boston-pm
>
>
> Duane Bronson
> [email protected]
> http://www.nerdlogic.com/
> 5 Goden St.
> Belmont, MA 02478
> 617.515.2909
>
>
> _______________________________________________
> Boston-pm mailing list
> [email protected]
> http://mail.pm.org/mailman/listinfo/boston-pm
>


-- 



_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to