[Parrot-users] How to add support for _dumper pretty printing to a class

Austin Hastings austin_hastings at yahoo.com
Sat Jan 9 23:23:14 UTC 2010


The easiest way is just to call it. If you class is hash based, dumper knows about hashes and will dump it okay. You'll have to import the code from dumper.pbc if that hasn't been done already.

If you're using something else, add a method __dump (you may have to make it an "our method" in -rx) that does what you want.

Here's one I wrote for Class::ArrayBased. Please note that this is nqp, not nqp-rx, and I haven't paid the tax on it yet. (Sorry, there was a holiday...)

method __dump($dumper, $label) {
        my $subindent;
        my $indent;

        # (subindent, indent) = dumper."newIndent"()
        Q:PIR {
                .local string indent, subindent
                $P0 = find_lex '$dumper'
                (subindent, indent) = $P0.'newIndent'()
                $P0 = box subindent
                store_lex '$subindent', $P0
                $P0 = box indent
                store_lex '$indent', $P0
        };
        
        my $brace := '{';
        
        my $index:= 0;
        my $limit := Array::elements(self);
        
        while $index < $limit {
                print($brace, "\n", $subindent);
                $brace := '';
                
                my $val := self[$index];
        
                print("[", $index, "] => ");
                $dumper.dump($label, $val);
                
                $index++;
        }
        
        if $brace {
                print("(no attributes set)");
        } 
        else {
                print("\n", $indent, '}');
        }
        
        $dumper.deleteIndent();
}



--- On Sat, 1/9/10, cconstantine <cconstan at gmail.com> wrote:

> From: cconstantine <cconstan at gmail.com>
> Subject: [Parrot-users] How to add support for _dumper pretty printing to a class
> To: parrot-users at lists.parrot.org
> Date: Saturday, January 9, 2010, 10:12 AM
> I've created a class and I want to be
> able to dump it with _dumper.
> How would I go about doing that?
> _______________________________________________
> Parrot-users mailing list
> Parrot-users at lists.parrot.org
> http://lists.parrot.org/mailman/listinfo/parrot-users
> 


More information about the Parrot-users mailing list