| 1 | package MYDan::Node::Object; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | MYDan::Node::Object - MYDan::Node Object Interface. | ||
| 6 | |||
| 7 | Each Captialized method depends on a corresponding Lvalue implementation. | ||
| 8 | |||
| 9 | e.g. Add() depends on child class add(). | ||
| 10 | |||
| 11 | =cut | ||
| 12 | use strict; | ||
| 13 | use warnings; | ||
| 14 | |||
| 15 | use overload | ||
| 16 | '=' => \&assign, | ||
| 17 | '+' => \&Add, | ||
| 18 | '-' => \&Subtract, | ||
| 19 | '*' => \&Multiply, | ||
| 20 | '&' => \&Intersect, | ||
| 21 | '^' => \&Symdiff, | ||
| 22 | '""' => \&Dump, | ||
| 23 | '@{}' => \&List, | ||
| 24 | bool => \&bool, | ||
| 25 | '==' => \&same, | ||
| 26 | '!=' => sub { ! same( @_ ) }; | ||
| 27 | |||
| 28 | =head1 METHODS | ||
| 29 | |||
| 30 | =head3 clone() | ||
| 31 | |||
| 32 | Returns a cloned object. Requires a load() method. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | sub clone | ||
| 36 | { | ||
| 37 | my $self = shift; | ||
| 38 | my $clone = $self->new()->load( $self ); | ||
| 39 | } | ||
| 40 | |||
| 41 | =head3 assign() | ||
| 42 | |||
| 43 | Overloads B<=>. Returns the object itself. | ||
| 44 | |||
| 45 | =cut | ||
| 46 | sub assign { $_[0] } | ||
| 47 | |||
| 48 | =head3 bool() | ||
| 49 | |||
| 50 | Overloads B<bool>. Returns I<true> if object is defined, I<false> otherwise. | ||
| 51 | |||
| 52 | =cut | ||
| 53 | sub bool { defined $_[0] } | ||
| 54 | |||
| 55 | =head3 same( object ) | ||
| 56 | |||
| 57 | Overloads B<==>. ( And the inverse overloads B<!=> ). | ||
| 58 | Returns I<true> if two objects are the same, I<false> otherwise. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | sub same { overload::StrVal( $_[0] ) eq overload::StrVal( $_[1] ) } | ||
| 62 | |||
| 63 | =head3 List() | ||
| 64 | |||
| 65 | Overloads '@{}'. Returns a list. | ||
| 66 | |||
| 67 | =cut | ||
| 68 | sub List | ||
| 69 | { | ||
| 70 | my $self = shift; | ||
| 71 | return wantarray ? $self->list() : [ $self->list() ]; | ||
| 72 | } | ||
| 73 | |||
| 74 | =head3 Dump() | ||
| 75 | |||
| 76 | Overloads '""'. Returns a string expression. | ||
| 77 | |||
| 78 | =cut | ||
| 79 | sub Dump | ||
| 80 | { | ||
| 81 | my $self = shift; | ||
| 82 | return $self->dump(); | ||
| 83 | } | ||
| 84 | |||
| 85 | =head3 Add( $o ) | ||
| 86 | |||
| 87 | Overloads B<+>. Returns the union of two objects. | ||
| 88 | |||
| 89 | =head3 Subtract( $o ) | ||
| 90 | |||
| 91 | Overloads B<->. Returns the I<left> difference of two objects. | ||
| 92 | |||
| 93 | =head3 Intersect( $o ) | ||
| 94 | |||
| 95 | Overloads B<&>. Returns the intersection of two objects. | ||
| 96 | |||
| 97 | =head3 Symdiff( $o ) | ||
| 98 | |||
| 99 | Overloads B<^>. Returns the symmetric difference of two objects. | ||
| 100 | |||
| 101 | =head3 Multiply( $o ) | ||
| 102 | |||
| 103 | Overloads B<*>. Returns the product of two objects. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | sub AUTOLOAD | ||
| 107 | { | ||
| 108 | my $self = shift; | ||
| 109 | my $op = our $AUTOLOAD =~ /::(Add|Subtract|Interact|Multiply|Symdiff)$/ | ||
| 110 | ? $self->can( lcfirst $1 ) : undef; | ||
| 111 | return $op ? $self->clone()->$op( @_ ) : $self; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub DESTROY { } | ||
| 115 | |||
| 116 | 1; |