| 1 | # Copyright (c) 2026 Philipp Schafft | ||
| 2 | |||
| 3 | # licensed under Artistic License 2.0 (see LICENSE file) | ||
| 4 | |||
| 5 | # ABSTRACT: module to interact with the words of any language | ||
| 6 | |||
| 7 | |||
| 8 | package Lingua::Generic::Interface::Word; | ||
| 9 | |||
| 10 | use v5.20; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | use Data::Identifier v0.34; | ||
| 16 | |||
| 17 | our $VERSION = v0.02; | ||
| 18 | |||
| 19 | use parent qw(Data::Identifier::Interface::Subobjects Data::Identifier::Interface::Simple); | ||
| 20 | |||
| 21 | use overload ( | ||
| 22 | '""' => \&as_string, | ||
| 23 | 'eq' => sub { $_[0]->eq($_[1]) }, | ||
| 24 | 'ne' => sub { !$_[0]->eq($_[1]) }, | ||
| 25 | 'cmp' => sub { $_[0]->cmp($_[1]) }, | ||
| 26 | ); | ||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | sub new { | ||
| 31 | my ($pkg, $type, $value, @opts) = @_; | ||
| 32 | |||
| 33 | croak 'No type given' unless defined $type; | ||
| 34 | croak 'No value given' unless defined $value; | ||
| 35 | croak 'Stray options passed' if scalar @opts; | ||
| 36 | |||
| 37 | if ($type eq 'from') { | ||
| 38 | if (ref $value) { | ||
| 39 | if ($value->isa(__PACKAGE__)) { | ||
| 40 | return $value; | ||
| 41 | } else { | ||
| 42 | ... | ||
| 43 | } | ||
| 44 | } else { | ||
| 45 | $type = 'string'; | ||
| 46 | } | ||
| 47 | } elsif ($type eq 'ise') { | ||
| 48 | ... | ||
| 49 | } | ||
| 50 | |||
| 51 | if ($type eq 'string') { | ||
| 52 | return bless { | ||
| 53 | string => $value, | ||
| 54 | }, $pkg; | ||
| 55 | } else { | ||
| 56 | croak 'Bad type: '.$type; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | sub as_string { | ||
| 62 | my ($self, @opts) = @_; | ||
| 63 | return $self->{string} // confess 'BUG: No valid string'; | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | sub eq { | ||
| 68 | my ($self, $other, @opts) = @_; | ||
| 69 | |||
| 70 | croak 'Stray options passed' if scalar @opts; | ||
| 71 | |||
| 72 | return 1 if !defined($self) && !defined($other); | ||
| 73 | return undef unless defined($self) && defined($other); | ||
| 74 | |||
| 75 | $self = __PACKAGE__->new(from => $self) unless eval {$self->isa(__PACKAGE__)}; | ||
| 76 | $other = __PACKAGE__->new(from => $other) unless eval {$other->isa(__PACKAGE__)}; | ||
| 77 | |||
| 78 | return undef unless ref($self) eq ref($other); | ||
| 79 | |||
| 80 | return $self->as_string eq $other->as_string; | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | sub cmp { | ||
| 85 | my ($self, $other, @opts) = @_; | ||
| 86 | |||
| 87 | croak 'Stray options passed' if scalar @opts; | ||
| 88 | |||
| 89 | return 1 if !defined($self) && !defined($other); | ||
| 90 | return undef unless defined($self) && defined($other); | ||
| 91 | |||
| 92 | $self = __PACKAGE__->new(from => $self) unless eval {$self->isa(__PACKAGE__)}; | ||
| 93 | $other = __PACKAGE__->new(from => $other) unless eval {$other->isa(__PACKAGE__)}; | ||
| 94 | |||
| 95 | { | ||
| 96 | my $v = ref($self) cmp ref($other); | ||
| 97 | return $v if $v != 0; | ||
| 98 | } | ||
| 99 | |||
| 100 | { | ||
| 101 | my $str_self = $self->as_string; | ||
| 102 | my $str_other = $other->as_string; | ||
| 103 | |||
| 104 | return $str_self cmp $str_other; | ||
| 105 | } | ||
| 106 | |||
| 107 | croak 'BUG!'; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | sub natural_language { | ||
| 112 | my ($self) = @_; | ||
| 113 | ... | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | sub displayname { | ||
| 118 | my ($self, @opts) = @_; | ||
| 119 | return $self->as_string if scalar(@opts) == 0; | ||
| 120 | { # work around the case we have no working $self->as(). | ||
| 121 | my %x = @opts; | ||
| 122 | delete $x{default}; | ||
| 123 | delete $x{no_defaults}; | ||
| 124 | return $self->as_string if scalar(keys %x) == 0; | ||
| 125 | } | ||
| 126 | return $self->as('Data::Identifier')->displayname(@opts); | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | sub ise { | ||
| 131 | ... | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | # ---- Private helpers ---- | ||
| 138 | |||
| 139 | 1; | ||
| 140 | |||
| 141 | __END__ |