| 1 | # Copyright (c) 2025-2026 Philipp Schafft | ||
| 2 | |||
| 3 | # licensed under Artistic License 2.0 (see LICENSE file) | ||
| 4 | |||
| 5 | # ABSTRACT: module to interact with the famibeib fragments | ||
| 6 | |||
| 7 | |||
| 8 | package Lingua::famibeib::Fragment; | ||
| 9 | |||
| 10 | use v5.16; | ||
| 11 | use strict; | ||
| 12 | use warnings; | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | |||
| 16 | use Lingua::famibeib::Word; | ||
| 17 | |||
| 18 | our $VERSION = v0.07; | ||
| 19 | |||
| 20 | use parent 'Data::Identifier::Interface::Subobjects'; | ||
| 21 | |||
| 22 | |||
| 23 | sub new { | ||
| 24 | my ($pkg, $type, $value, @opts) = @_; | ||
| 25 | my @words; | ||
| 26 | my $self = bless {words => \@words}, $pkg; | ||
| 27 | |||
| 28 | croak 'Stray options passed' if scalar @opts; | ||
| 29 | croak 'No type given' unless defined $type; | ||
| 30 | croak 'No value given' unless defined $value; | ||
| 31 | |||
| 32 | if ($type eq 'words') { | ||
| 33 | foreach my $word (@{$value}) { | ||
| 34 | push(@words, Lingua::famibeib::Word->new(from => $word)); | ||
| 35 | } | ||
| 36 | } else { | ||
| 37 | croak 'Bad type: '.$type; | ||
| 38 | } | ||
| 39 | |||
| 40 | return $self; | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | sub words { | ||
| 45 | my ($self, @opts) = @_; | ||
| 46 | |||
| 47 | croak 'Stray options passed' if scalar @opts; | ||
| 48 | |||
| 49 | return @{$self->{words}}; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | sub as_string { | ||
| 54 | my ($self, @opts) = @_; | ||
| 55 | |||
| 56 | croak 'Stray options passed' if scalar @opts; | ||
| 57 | |||
| 58 | return $self->{string} //= join(' ', @{$self->{words}}); | ||
| 59 | } | ||
| 60 | |||
| 61 | # ---- Private helpers ---- | ||
| 62 | |||
| 63 | 1; | ||
| 64 | |||
| 65 | __END__ | ||
| 66 | |||
| 67 | =pod | ||
| 68 | |||
| 69 | =encoding UTF-8 | ||
| 70 | |||
| 71 | =head1 NAME | ||
| 72 | |||
| 73 | Lingua::famibeib::Fragment - module to interact with the famibeib fragments | ||
| 74 | |||
| 75 | =head1 VERSION | ||
| 76 | |||
| 77 | version v0.07 | ||
| 78 | |||
| 79 | =head1 SYNOPSIS | ||
| 80 | |||
| 81 | use Lingua::famibeib::Fragment; | ||
| 82 | |||
| 83 | This module inherits from L<Data::Identifier::Interface::Subobjects>. | ||
| 84 | |||
| 85 | =head1 METHODS | ||
| 86 | |||
| 87 | =head2 new | ||
| 88 | |||
| 89 | my Lingua::famibeib::Fragment $fragment = Lingua::famibeib::Fragment->new($type => $value); | ||
| 90 | # e.g.: | ||
| 91 | my Lingua::famibeib::Fragment $fragment = Lingua::famibeib::Fragment->new(words => [...]); | ||
| 92 | |||
| 93 | (since v0.01) | ||
| 94 | |||
| 95 | Creates a new fragment instance. | ||
| 96 | A fragment is any string of words. | ||
| 97 | |||
| 98 | Currently the following types (C<$type>) are supported: | ||
| 99 | |||
| 100 | =over | ||
| 101 | |||
| 102 | =item C<words> | ||
| 103 | |||
| 104 | Constructs a fragment from an arrayref of words (instances of L<Lingua::famibeib::Word>, or parsed as per L<Lingua::famibeib::Word/new>'s C<from>). | ||
| 105 | |||
| 106 | =back | ||
| 107 | |||
| 108 | =head2 words | ||
| 109 | |||
| 110 | my @words = $fragment->words; | ||
| 111 | |||
| 112 | (since v0.01) | ||
| 113 | |||
| 114 | Returns the list of words as found in the fragment. | ||
| 115 | |||
| 116 | =head2 as_string | ||
| 117 | |||
| 118 | my $str = $fragment->as_string; | ||
| 119 | |||
| 120 | (since v0.01) | ||
| 121 | |||
| 122 | Returns the current fragment as a string. | ||
| 123 | |||
| 124 | =head1 AUTHOR | ||
| 125 | |||
| 126 | Philipp Schafft <lion@cpan.org> | ||
| 127 | |||
| 128 | =head1 COPYRIGHT AND LICENSE | ||
| 129 | |||
| 130 | This software is Copyright (c) 2025-2026 by Philipp Schafft <lion@cpan.org>. | ||
| 131 | |||
| 132 | This is free software, licensed under: | ||
| 133 | |||
| 134 | The Artistic License 2.0 (GPL Compatible) | ||
| 135 | |||
| 136 | =cut |