File: Streams.pm

package info (click to toggle)
libcourriel-perl 0.49-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,312 kB
  • sloc: perl: 2,526; sh: 23; makefile: 7
file content (48 lines) | stat: -rw-r--r-- 814 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package Courriel::Role::Streams;

use strict;
use warnings;
use namespace::autoclean;

our $VERSION = '0.49';

use Courriel::Types qw( Streamable );
use Params::ValidationCompiler qw( validation_for );

use Moose::Role;

{
    my $validator = validation_for(
        params        => [ output => { type => Streamable } ],
        named_to_list => 1,
    );

    sub stream_to {
        my $self = shift;
        my ($output) = $validator->(@_);

        $self->_stream_to($output);

        return;
    }
}

sub as_string {
    my $self = shift;

    my $string = q{};

    $self->stream_to( output => $self->_string_output( \$string ) );

    return $string;
}

sub _string_output {
    my $self      = shift;
    my $stringref = shift;

    my $string = q{};
    return sub { ${$stringref} .= $_ for @_ };
}

1;