view release on metacpan or search on metacpan
- Stream: Fix schemas, all streams must be arrays.
0.68 2016-01-22 Released-By: PERLANCAR
- [Incompatible change] Rename functions to make them clearer.
0.67 2016-01-22 Released-By: PERLANCAR
- Add function: Stream::num_words to test validation of streaming
output.
0.66 2016-01-22 Released-By: PERLANCAR
- Add functions: Stream::word, Stream::word_err to test validation of
streaming output.
0.65 2016-01-20 Released-By: PERLANCAR
- [Bugfix] Fix metadata of Stream::wc & Stream::wc_keys.
0.64 2016-01-19 Released-By: PERLANCAR
- Add module: Perinci::Examples::Table.
0.63 2015-11-28 Released-By: PERLANCAR
- Add function: Tiny::hello_naked.
0.62 2015-11-27 Released-By: PERLANCAR
- Add module: Perinci::Examples::CSV.
- Add more input streaming examples: square_input_from_stdin,
square_input_from_stdin_or_file, square_input_from_file.
0.61 2015-09-29 Released-By: PERLANCAR
- Add function: ::Tiny::Args::as_is.
0.60 2015-09-26 Released-By: PERLANCAR
- Add options --num (-n) to nat() & hash_stream().
0.48 2015-01-20 Released-By: PERLANCAR
- Add module: Perinci::Examples::Stream.
0.47 2015-01-17 Released-By: PERLANCAR
- Update to Rinci 1.1.71: FileStream now uses coderef for streaming
input/output instead of filehandle.
0.46 2015-01-11 Released-By: PERLANCAR
- No functional changes.
- Add description to functions in Perinci::Examples::FileStream.
- Typo fixes.
lib/Perinci/Examples/FilePartial.pm view on Meta::CPAN
summary => 'Examples for reading/writing files (demos partial argument/result)',
description => <<'_',
The functions in this package demoes partial content upload as well as partial
result.
The functions are separated into this module because these functions read/write
files on the filesystem and might potentially be dangerous if
`Perinci::Examples` is exposed to the network by accident.
See also `Perinci::Examples::FileStream` which uses streaming instead of
partial.
_
};
$SPEC{read_file} = {
v => 1.1,
args => {
path => {schema=>'str*', req=>1, pos=>0},
},
lib/Perinci/Examples/FilePartial.pm view on Meta::CPAN
=head1 DESCRIPTION
The functions in this package demoes partial content upload as well as partial
result.
The functions are separated into this module because these functions read/write
files on the filesystem and might potentially be dangerous if
C<Perinci::Examples> is exposed to the network by accident.
See also C<Perinci::Examples::FileStream> which uses streaming instead of
partial.
=head1 FUNCTIONS
=head2 append_file
Usage:
append_file(%args) -> [$status_code, $reason, $payload, \%result_meta]
lib/Perinci/Examples/FileStream.pm view on Meta::CPAN
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2024-07-17'; # DATE
our $DIST = 'Perinci-Examples'; # DIST
our $VERSION = '0.825'; # VERSION
our %SPEC;
$SPEC{':package'} = {
v => 1.1,
summary => 'Examples for reading/writing files (using streaming result)',
description => <<'_',
The functions in this package demonstrate byte streaming of input and output.
The functions are separated into this module because these functions read/write
files on the filesystem and might potentially be dangerous if
<pm:Perinci::Examples> is exposed to the network by accident.
See also <pm:Perinci::Examples::FilePartial> which uses partial technique
instead of streaming.
_
};
$SPEC{read_file} = {
v => 1.1,
args => {
path => {schema=>'str*', req=>1, pos=>0},
},
result => {schema=>'buf*', stream=>1},
description => <<'_',
This function demonstrate output streaming of bytes.
To do output streaming, on the function side, you just return a coderef which
can be called by caller (e.g. CLI framework <pm:Perinci::CmdLine>) to read data
from. Code must return data or undef to signify exhaustion.
This works over remote (HTTP) too, because output streaming is supported by
<pod:Riap::HTTP> (version 1.2) and <pm:Perinci::Access::HTTP::Client>. Streams
are translated into HTTP chunks.
_
};
sub read_file {
my %args = @_; my $arg_err; { no warnings ('void');if (exists($args{'path'})) { ((defined($args{'path'})) ? 1 : (($arg_err //= "Required but not specified"),0)) && ((!ref($args{'path'})) ? 1 : (($arg_err //= "Not of type text"),0)); if ($arg_err)...
my $path = $args{path};
(-f $path) or return [404, "No such file '$path'"];
open my($fh), "<", $path or return [500, "Can't open '$path': $!"];
[200, "OK", sub { scalar <$fh> }, {stream=>1}];
}
$SPEC{write_file} = {
v => 1.1,
description => <<'_',
This function demonstrates input streaming of bytes.
To do input streaming, on the function side, you just specify one your args with
the `stream` property set to true (`stream => 1`). In this example, the
`content` argument is set to streaming.
If you run the function through <pm:Perinci::CmdLine>, you'll get a coderef
instead of the actual value. You can then repeatedly call the code to read data.
This currently works for local functions only. As of this writing,
<pod:Riap::HTTP> protocol does not support input streaming. It supports partial
input though (see the documentation on how this works) and theoretically
streaming can be emulated by client library using partial input. However, client
like <pm:Perinci::Access::HTTP::Client> does not yet support this.
Note that the argument's schema is still `buf*`, not `code*`.
Note: This function overwrites existing file.
_
args => {
path => {schema=>'str*', req=>1, pos=>0},
content => {schema=>'buf*', req=>1, pos=>1, stream=>1,
lib/Perinci/Examples/FileStream.pm view on Meta::CPAN
}
} else {
print $fh $content;
$written += length($content);
}
[200, "Appended $written byte(s)"];
}
1;
# ABSTRACT: Examples for reading/writing files (using streaming result)
__END__
=pod
=encoding UTF-8
=head1 NAME
Perinci::Examples::FileStream - Examples for reading/writing files (using streaming result)
=head1 VERSION
This document describes version 0.825 of Perinci::Examples::FileStream (from Perl distribution Perinci-Examples), released on 2024-07-17.
=head1 DESCRIPTION
The functions in this package demonstrate byte streaming of input and output.
The functions are separated into this module because these functions read/write
files on the filesystem and might potentially be dangerous if
L<Perinci::Examples> is exposed to the network by accident.
See also L<Perinci::Examples::FilePartial> which uses partial technique
instead of streaming.
=head1 FUNCTIONS
=head2 append_file
Usage:
append_file(%args) -> [$status_code, $reason, $payload, \%result_meta]
lib/Perinci/Examples/FileStream.pm view on Meta::CPAN
Return value: (any)
=head2 read_file
Usage:
read_file(%args) -> [$status_code, $reason, $payload, \%result_meta]
This function demonstrate output streaming of bytes.
To do output streaming, on the function side, you just return a coderef which
can be called by caller (e.g. CLI framework L<Perinci::CmdLine>) to read data
from. Code must return data or undef to signify exhaustion.
This works over remote (HTTP) too, because output streaming is supported by
L<Riap::HTTP> (version 1.2) and L<Perinci::Access::HTTP::Client>. Streams
are translated into HTTP chunks.
This function is not exported.
Arguments ('*' denotes required arguments):
=over 4
=item * B<path>* => I<str>
lib/Perinci/Examples/FileStream.pm view on Meta::CPAN
Return value: (buf)
=head2 write_file
Usage:
write_file(%args) -> [$status_code, $reason, $payload, \%result_meta]
This function demonstrates input streaming of bytes.
To do input streaming, on the function side, you just specify one your args with
the C<stream> property set to true (C<< stream =E<gt> 1 >>). In this example, the
C<content> argument is set to streaming.
If you run the function through L<Perinci::CmdLine>, you'll get a coderef
instead of the actual value. You can then repeatedly call the code to read data.
This currently works for local functions only. As of this writing,
L<Riap::HTTP> protocol does not support input streaming. It supports partial
input though (see the documentation on how this works) and theoretically
streaming can be emulated by client library using partial input. However, client
like L<Perinci::Access::HTTP::Client> does not yet support this.
Note that the argument's schema is still C<buf*>, not C<code*>.
Note: This function overwrites existing file.
This function is not exported.
Arguments ('*' denotes required arguments):
lib/Perinci/Examples/Stream.pm view on Meta::CPAN
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
our $DATE = '2024-07-17'; # DATE
our $DIST = 'Perinci-Examples'; # DIST
our $VERSION = '0.825'; # VERSION
our %SPEC;
$SPEC{':package'} = {
v => 1.1,
summary => 'Examples for streaming input/output',
description => <<'_',
This package contains functions that demonstrate streaming input/output.
_
};
my %arg_num = (
num => {
summary => 'Limit number of entries to produce',
description => <<'_',
The default is to produce an infinite number.
lib/Perinci/Examples/Stream.pm view on Meta::CPAN
_
schema => ['int*', min=>0],
cmdline_aliases => {n=>{}},
pos => 0,
},
);
$SPEC{produce_ints} = {
v => 1.1,
summary => 'This function produces a stream of integers, starting from 1',
tags => ['category:streaming-result'],
args => {
%arg_num,
},
result => {
stream => 1,
schema => ['array*', of=>'int*'],
},
};
sub produce_ints {
my %args = @_;
lib/Perinci/Examples/Stream.pm view on Meta::CPAN
my $num = $args{num};
[200, "OK", sub {
return undef if defined($num) && $i > $num; ## no critic: Subroutines::ProhibitExplicitReturnUndef
$i++;
}];
}
$SPEC{count_ints} = {
v => 1.1,
summary => 'This function accepts a stream of integers and return the number of integers input',
tags => ['category:streaming-input'],
args => {
input => {
summary => 'Numbers',
schema => ['array*', of=>'int*'],
stream => 1,
cmdline_src => 'stdin_or_files',
},
},
};
sub count_ints {
my %args = @_;
my $input = $args{input};
my $n = 0;
$n++ while defined $input->();
[200, "OK", $n];
}
$SPEC{count_lines} = {
v => 1.1,
summary => 'Count number of lines in the input',
tags => ['category:streaming-input'],
args => {
input => {
summary => 'Lines',
schema => ['array*', of=>'str*'],
stream => 1,
cmdline_src => 'stdin_or_files',
},
},
};
sub count_lines {
my %args = @_;
my $input = $args{input};
my $n = 0;
$n++ while defined $input->();
[200, "OK", "Input is $n line(s)"];
}
$SPEC{produce_words} = {
v => 1.1,
summary => 'This function produces a stream of random words',
tags => ['category:streaming-result'],
args => {
%arg_num,
},
result => {
stream => 1,
schema => ['array*', of=>['str*', match=>'\A\w+\z']],
},
};
sub produce_words {
my %args = @_;
lib/Perinci/Examples/Stream.pm view on Meta::CPAN
[200, "OK", sub {
return undef if defined($num) && $i > $num; ## no critic: Subroutines::ProhibitExplicitReturnUndef
$i++;
join('', map { ['a'..'z']->[26*rand()] } 1..(int(6*rand)+5));
}];
}
$SPEC{produce_words_err} = {
v => 1.1,
summary => 'Like `produce_words()`, but 1 in every 10 words will be a non-word (which fails the result schema)',
tags => ['categoryr:streaming-result'],
args => {
%arg_num,
},
result => {
stream => 1,
schema => ['array*', of => ['str*', match=>'\A\w+\z']],
},
};
sub produce_words_err {
my %args = @_;
lib/Perinci/Examples/Stream.pm view on Meta::CPAN
"contain space";
} else {
join('', map { ['a'..'z']->[26*rand()] } 1..(int(6*rand)+5));
}
}];
}
$SPEC{count_words} = {
v => 1.1,
summary => 'This function receives a stream of words and return the number of words',
tags => ['category:streaming-input'],
description => <<'_',
Input validation will check that each record from the stream is a word.
_
args => {
input => {
schema => ['array*', of=>['str*', match=>'\A\w+\z']],
stream => 1,
cmdline_src => 'stdin_or_files',
lib/Perinci/Examples/Stream.pm view on Meta::CPAN
my $i = 1;
[200, "OK", sub {
return undef if defined($num) && $i > $num; ## no critic: Subroutines::ProhibitExplicitReturnUndef
{num=>$i++};
}];
}
$SPEC{square_nums} = {
v => 1.1,
summary => 'This function squares its stream input',
tags => ['category:streaming-input', 'category:streaming-result'],
args => {
input => {
req => 1,
stream => 1,
schema => ['array*', of=>'float*'],
cmdline_src => 'stdin_or_files',
},
},
result => {
stream => 1,
lib/Perinci/Examples/Stream.pm view on Meta::CPAN
[200, "OK", sub {
my $n = $input->();
return undef unless defined $n; ## no critic: Subroutines::ProhibitExplicitReturnUndef
$n*$n;
}];
}
$SPEC{square_nums_from_file} = {
v => 1.1,
summary => 'This function squares its stream input',
tags => ['category:streaming-input', 'category:streaming-result'],
args => {
input => {
req => 1,
pos => 0,
stream => 1,
schema => ['array*', of=>'float*'],
cmdline_src => 'file',
},
},
result => {
lib/Perinci/Examples/Stream.pm view on Meta::CPAN
schema => ['array*', of=>'float*'],
},
};
sub square_nums_from_file {
goto &square_input;
}
$SPEC{square_nums_from_stdin} = {
v => 1.1,
summary => 'This function squares its stream input',
tags => ['category:streaming-input', 'category:streaming-result'],
args => {
input => {
req => 1,
pos => 0,
stream => 1,
schema => ['array*', of=>'float*'],
cmdline_src => 'stdin',
},
},
result => {
lib/Perinci/Examples/Stream.pm view on Meta::CPAN
schema => ['array*', of=>'float*'],
},
};
sub square_nums_from_stdin {
goto &square_input;
}
$SPEC{square_nums_from_stdin_or_file} = {
v => 1.1,
summary => 'This function squares its stream input',
tags => ['category:streaming-input', 'category:streaming-result'],
args => {
input => {
req => 1,
pos => 0,
stream => 1,
schema => 'float*',
cmdline_src => 'stdin_or_file',
},
},
result => {
lib/Perinci/Examples/Stream.pm view on Meta::CPAN
schema => ['array*', of=>'float*'],
},
};
sub square_nums_from_stdin_or_file {
goto &square_input;
}
$SPEC{wc} = {
v => 1.1,
summary => 'Count the number of lines/words/characters of input, like the "wc" command',
tags => ['category:streaming-input'],
args => {
input => {
req => 1,
stream => 1,
schema => ['array*', of=>'str*'],
cmdline_src => 'stdin_or_files',
'cmdline.chomp' => 0,
},
},
result => {
lib/Perinci/Examples/Stream.pm view on Meta::CPAN
$lines++;
$words++ for $line =~ /(\S+)/g;
$chars += length($line);
}
[200, "OK", {lines=>$lines, words=>$words, chars=>$chars}];
}
$SPEC{wc_keys} = {
v => 1.1,
summary => 'Count the number of keys of each hash',
tags => ['category:streaming-input'],
description => <<'_',
This is a simple demonstration of accepting a stream of hashes. In command-line
application this will translate to JSON stream.
_
args => {
input => {
req => 1,
stream => 1,
lib/Perinci/Examples/Stream.pm view on Meta::CPAN
my $input = $args{input};
my ($keys) = (0);
while (defined(my $hash = $input->())) {
$keys += keys %$hash;
}
[200, "OK", {keys=>$keys}];
}
1;
# ABSTRACT: Examples for streaming input/output
__END__
=pod
=encoding UTF-8
=head1 NAME
Perinci::Examples::Stream - Examples for streaming input/output
=head1 VERSION
This document describes version 0.825 of Perinci::Examples::Stream (from Perl distribution Perinci-Examples), released on 2024-07-17.
=head1 DESCRIPTION
This package contains functions that demonstrate streaming input/output.
=head1 FUNCTIONS
=head2 count_ints
Usage:
count_ints(%args) -> [$status_code, $reason, $payload, \%result_meta]