| 1 | 9 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 2 | 9 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | =head1 NAME | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | Data::SExpression::Symbol -- A Lisp symbol read by Data::SExpression | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 DESCRIPTION | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | A Data::SExpression::Symbol represents a lisp symbol. Symbols are | ||||||||||||||||||||
| 11 | usually used as opaque objects that can be compared with each other, | ||||||||||||||||||||
| 12 | but are not intended to be used for other operations. | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | There are two kinds of symbols, C<interned>, and C<uninterned>. Most | ||||||||||||||||||||
| 15 | symbols are C<interned>. There is only ever one C<interned> instance | ||||||||||||||||||||
| 16 | of the C<Symbol> class for a given name. | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 STRINGIFICATION AND COMPARISON | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | Interned symbols stringify to their ->name. Uninterned symbols | ||||||||||||||||||||
| 21 | stringify to "#:$name", after the Common Lisp convention. | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | Interned symbols are eq to their name. Uninterned symbols are not eq | ||||||||||||||||||||
| 24 | to anything except themself. | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | =cut | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | package Data::SExpression::Symbol; | ||||||||||||||||||||
| 29 | 9 | ▶ | use base qw(Class::Accessor::Fast); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 30 | 9 | __PACKAGE__->mk_ro_accessors(qw(interned name)); | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 9 | ▶ | use Scalar::Util qw(blessed refaddr); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 9 | ▶ | use overload q{""} => \&stringify, | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | eq => \&equal, | ||||||||||||||||||||
| 36 | ne => \¬_equal, | ||||||||||||||||||||
| 37 | 9 | fallback => 1; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 9 | our %INTERN; | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | =head2 new NAME | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | Returns a new interned symbol with the given NAME | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | =cut | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 3 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 48 | 3 | my $class = shift; | |||||||||||||||||||
| 49 | 3 | my $name = shift; | |||||||||||||||||||
| 50 | 3 | ▶ | return $INTERN{$name} if $INTERN{$name}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 2 | my $self = { | |||||||||||||||||||
| 52 | interned => 1, | ||||||||||||||||||||
| 53 | name => $name | ||||||||||||||||||||
| 54 | }; | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 2 | bless($self, $class); | |||||||||||||||||||
| 57 | 2 | $INTERN{$name} = $self; | |||||||||||||||||||
| 58 | 2 | return $self; | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =head2 uninterned NAME | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | Returns a new uninterned symbol with the given NAME | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =cut | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 2 | ▶ | sub uninterned { | ||||||||||||||||||
|
Subroutine
uninterned: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 68 | 2 | my $class = shift; | |||||||||||||||||||
| 69 | 2 | my $name = shift; | |||||||||||||||||||
| 70 | 2 | return bless({interned => 0, name => $name}, $class); | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =head2 name | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | Returns the symbol's name, as passed to C<new> or C<uninterned>. | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =head2 interned | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | Returned true iff the symbol is interned | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 4 | ▶ | sub stringify { | ||||||||||||||||||
|
Subroutine
stringify: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 84 | 4 | my $self = shift; | |||||||||||||||||||
| 85 | 4 | ▶ | return ($self->interned ? "" : "#:") . $self->name; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 7 | ▶ | sub equal { | ||||||||||||||||||
|
Subroutine
equal: called CC=7 SCAR=20.2
Pod
undocumented
| |||||||||||||||||||||
| 89 | 7 | my $self = shift; | |||||||||||||||||||
| 90 | 7 | my $other = shift; | |||||||||||||||||||
| 91 | 7 | ▶ | if(!$self->interned) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 5 | ▶ | return blessed($other) && refaddr($self) == refaddr($other); | ||||||||||||||||||
|
Condition100%3/3
builtin::blessed($other) && builtin::refaddr($self) == builtin::refaddr($other)
MC/DC100%2/2
builtin::blessed($other) && builtin::refaddr($self) == builtin::refaddr($other)
builtin::blessed($other)
builtin::refaddr($self) == builtin::refaddr($other)
Truth table100%3/3
A && B
A= builtin::blessed($other)B= builtin::refaddr($self) == builtin::refaddr($other)
| |||||||||||||||||||||
| 93 | } else { | ||||||||||||||||||||
| 94 | 2 | ▶ | if(!ref($other)) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 95 | 1 | return $self->name eq $other; | |||||||||||||||||||
| 96 | } elsif(blessed($other)) { | ||||||||||||||||||||
| 97 | 1 | ▶ | if($other->isa(__PACKAGE__)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | 1 | ▶ | return $other->interned && ($self->name eq $other->name); | ||||||||||||||||||
|
Condition33%1/3
$other->interned && $self->name eq $other->name
MC/DC0%0/2
$other->interned && $self->name eq $other->name
$other->interned
$self->name eq $other->name
Truth table33%1/3
A && B
A= $other->internedB= $self->name eq $other->name
| |||||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | 0 | return; | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 4 | ▶ | sub not_equal { | ||||||||||||||||||
|
Subroutine
not_equal: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 106 | 4 | my $self = shift; | |||||||||||||||||||
| 107 | 4 | my $other = shift; | |||||||||||||||||||
| 108 | 4 | return !$self->equal($other); | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | =head1 SEE ALSO | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | L<Data::SExpression> | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =head1 AUTHOR | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | Nelson Elhage <nelhage@mit.edu> | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 9 | 1; | |||||||||||||||||||