| 1 | package Ryu; | ||
| 2 | # ABSTRACT: stream and data flow handling for async code | ||
| 3 | 37 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 37 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | # Older versions cannot complete the test suite successfully | ||
| 7 | 37 | ▶ | use 5.018; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 37 | our $VERSION = '4.001'; | |
| 10 | 37 | our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY | |
| 11 | |||
| 12 | =encoding utf8 | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Ryu - asynchronous stream building blocks | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | #!/usr/bin/env perl | ||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | use Ryu qw($ryu); | ||
| 24 | my ($lines) = | ||
| 25 | $ryu->from(\*STDIN) | ||
| 26 | ->by_line | ||
| 27 | ->filter(qr/\h/) | ||
| 28 | ->count | ||
| 29 | ->get; | ||
| 30 | print "Had $lines line(s) containing whitespace\n"; | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | Provides data flow processing for asynchronous coding purposes. It's a bit like L<ReactiveX|https://reactivex.io> in | ||
| 35 | concept. Where possible, it tries to provide a similar API. It is not a directly-compatible implementation, however. | ||
| 36 | |||
| 37 | For more information, start with L<Ryu::Source>. That's where most of the | ||
| 38 | useful parts are. | ||
| 39 | |||
| 40 | =head2 Why would I be using this? | ||
| 41 | |||
| 42 | Eventually some documentation pages might appear, but at the moment they're unlikely to exist. | ||
| 43 | |||
| 44 | =over 4 | ||
| 45 | |||
| 46 | =item * Network protocol implementations - if you're bored of stringing together C<substr>, C<pack>, C<unpack> | ||
| 47 | and C<vec>, try L<Ryu::Manual::Protocol> or L<Ryu::Buffer>. | ||
| 48 | |||
| 49 | =item * Extract, Transform, Load workflows (ETL) - need to pull data from somewhere, mangle it into shape, push it to | ||
| 50 | a database? that'd be L<Ryu::Manual::ETL> | ||
| 51 | |||
| 52 | =item * Reactive event handling - L<Ryu::Manual::Reactive> | ||
| 53 | |||
| 54 | =back | ||
| 55 | |||
| 56 | As an expert software developer with a keen eye for useful code, you may already be bored of this documentation | ||
| 57 | and on the verge of reaching for alternatives. The L</SEE ALSO> section may speed you on your way. | ||
| 58 | |||
| 59 | =head2 Compatibility | ||
| 60 | |||
| 61 | Since L<RxPerl> follows the ReactiveX conventions quite closely, we'd expect to have | ||
| 62 | the ability to connect L<RxPerl> observables to a L<Ryu::Source>, and provide an | ||
| 63 | adapter from a L<Ryu::Source> to act as an L<RxPerl>-style observable. This is not yet | ||
| 64 | implemented, but may be added in a future version. | ||
| 65 | |||
| 66 | Most of the other modules in L<SEE ALSO> are either not used widely enough or not a good | ||
| 67 | semantic fit for a compatibility layer - but if you're interested in this, | ||
| 68 | L<please ask about it|https://github.com/team-at-cpan/Ryu/issues> or provide patches! | ||
| 69 | |||
| 70 | =head2 Components | ||
| 71 | |||
| 72 | =head3 Sources | ||
| 73 | |||
| 74 | A source emits items. See L<Ryu::Source>. If in doubt, this is likely to be the class | ||
| 75 | that you wanted. | ||
| 76 | |||
| 77 | Items can be any scalar value - some examples: | ||
| 78 | |||
| 79 | =over 4 | ||
| 80 | |||
| 81 | =item * a single byte | ||
| 82 | |||
| 83 | =item * a character | ||
| 84 | |||
| 85 | =item * a byte string | ||
| 86 | |||
| 87 | =item * a character string | ||
| 88 | |||
| 89 | =item * an object instance | ||
| 90 | |||
| 91 | =item * an arrayref or hashref | ||
| 92 | |||
| 93 | =back | ||
| 94 | |||
| 95 | =head3 Sinks | ||
| 96 | |||
| 97 | A sink receives items. It's the counterpart to a source. See L<Ryu::Sink>. | ||
| 98 | |||
| 99 | =head3 Streams | ||
| 100 | |||
| 101 | A stream is a thing with a source. See L<Ryu::Stream>, which is likely to be something that does not yet | ||
| 102 | have much documentation - in practice, the L<Ryu::Source> implementation covers most use-cases. | ||
| 103 | |||
| 104 | =head2 So what does this module do? | ||
| 105 | |||
| 106 | Nothing. It's just a top-level loader for pulling in all the other components. | ||
| 107 | You wanted L<Ryu::Source> instead, or possibly L<Ryu::Buffer>. The other half of a | ||
| 108 | L<Ryu::Source> is a L<Ryu::Sink>. | ||
| 109 | |||
| 110 | =head2 Some notes that might not relate to anything | ||
| 111 | |||
| 112 | With a single parameter, L</from> and L</to> will use the given | ||
| 113 | instance as a L<Ryu::Source> or L<Ryu::Sink> respectively. | ||
| 114 | |||
| 115 | Multiple parameters are a shortcut for instantiating the given source | ||
| 116 | or sink: | ||
| 117 | |||
| 118 | my $stream = Ryu::Stream->from( | ||
| 119 | file => 'somefile.bin' | ||
| 120 | ); | ||
| 121 | |||
| 122 | is equivalent to | ||
| 123 | |||
| 124 | my $stream = Ryu::Stream->from( | ||
| 125 | Ryu::Source->new( | ||
| 126 | file => 'somefile.bin' | ||
| 127 | ) | ||
| 128 | ); | ||
| 129 | |||
| 130 | =head1 Why the name? | ||
| 131 | |||
| 132 | =over 4 | ||
| 133 | |||
| 134 | =item * C< $ryu > lines up with typical 4-character indentation settings. | ||
| 135 | |||
| 136 | =item * there's Rx for other languages, and this is based on the same ideas | ||
| 137 | |||
| 138 | =item * 流 was too hard for me to type | ||
| 139 | |||
| 140 | =back | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | 37 | ▶ | use Exporter qw(import export_to_level); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 145 | |||
| 146 | 37 | ▶ | use Ryu::Source; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 147 | 37 | ▶ | use Ryu::Sink; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 148 | |||
| 149 | 37 | our $ryu = __PACKAGE__->new; | |
| 150 | |||
| 151 | 37 | our @EXPORT_OK = qw($ryu); | |
| 152 | |||
| 153 | 9 | ▶ | our $FUTURE_FACTORY = sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 154 | 9 | Future->new->set_label($_[1]) | |
| 155 | 37 | }; | |
| 156 | |||
| 157 | =head1 METHODS | ||
| 158 | |||
| 159 | Note that you're more likely to find useful methods in the following classes: | ||
| 160 | |||
| 161 | =over 4 | ||
| 162 | |||
| 163 | =item * L<Ryu::Source> | ||
| 164 | |||
| 165 | =item * L<Ryu::Sink> | ||
| 166 | |||
| 167 | =item * L<Ryu::Observable> | ||
| 168 | |||
| 169 | =back | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | =head2 new | ||
| 174 | |||
| 175 | Instantiates a L<Ryu> object, allowing L</from>, L</just> and other methods. | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | 38 | ▶ | sub new { bless { @_[1..$#_] }, $_[0] } |
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||
| 180 | |||
| 181 | =head2 from | ||
| 182 | |||
| 183 | Helper method which returns a L<Ryu::Source> from a list of items. | ||
| 184 | |||
| 185 | =cut | ||
| 186 | |||
| 187 | 0 | ▶ | sub from { |
|
Subroutine
from: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 188 | 0 | my $self = shift; | |
| 189 | 0 | my $src = Ryu::Source->new; | |
| 190 | 0 | $src->from(@_) | |
| 191 | } | ||
| 192 | |||
| 193 | =head2 just | ||
| 194 | |||
| 195 | Helper method which returns a single-item L<Ryu::Source>. | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | 0 | ▶ | sub just { |
|
Subroutine
just: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 200 | 0 | my $self = shift; | |
| 201 | 0 | my $src = Ryu::Source->new; | |
| 202 | 0 | $src->from(shift); | |
| 203 | } | ||
| 204 | |||
| 205 | 37 | 1; | |
| 206 | |||
| 207 | __END__ | ||