README.md
1# NAME
2
3Plack::Handler::CLI - Command line interface to PSGI applications
4
5# VERSION
6
7This document describes Plack::Handler::CLI version 0.04.
8
9# SYNOPSIS
10
11 #!perl -w
12 # a cat(1) implementation on PSGI/CLI
13 use strict;
14 use Plack::Handler::CLI;
15 use URI::Escape qw(uri_unescape);
16
17 sub err {
18 my(@msg) = @_;
19 return [
20 500,
21 [ 'Content-Type' => 'text/plain' ],
22 \@msg,
23 ];
24 }
25
26 sub main {
27 my($env) = @_;
28
29 my @files = split '/', $env->{PATH_INFO};
30
31 local $/;
32
33 my @contents;
34 if(@files) {
35 foreach my $file(@files) {
36 my $f = uri_unescape($file);
37 open my $fh, '<', $f
38 or return err("Cannot open '$f': $!\n");
39
40 push @contents, readline($fh);
41 }
42 }
43 else {
44 push @contents, readline($env->{'psgi.input'});
45 }
46
47 return [
48 200,
49 [ 'Content-Type' => 'text/plain'],
50 \@contents,
51 ];
52 }
53
54 my $handler = Plack::Handler::CLI->new(need_headers => 0);
55 $handler->run(\&main);
56
57# DESCRIPTION
58
59Plack::Handler::CLI is a PSGI handler which provides a command line interface
60for PSGI applications.
61
62# INTERFACE
63
64## `Plack::Handler::CLI->new(%options)`
65
66Creates a Plack handler that implements a command line interface.
67
68PSGI headers will be printed by default, but you can suppress them
69by `need_headers => 0`.
70
71## `$cli->run(\&psgi_app, @argv) : Void`
72
73Runs _&psgi\_app_ with _@argv_.
74
75`"--key" => "value"` (or `"--key=value"`) pairs in _@argv_
76are packed into `QUERY_STRING`, while any other arguments are packed
77into `PATH_INFO`, so _&psgi\_app_ can get command line arguments as
78PSGI parameters. The first element of _@argv_ after the query parameters
79could also be a absolute URL.
80
81# DEPENDENCIES
82
83Perl 5.8.1 or later.
84
85# BUGS
86
87All complex software has bugs lurking in it, and this module is no
88exception. If you find a bug please either email me, or add the bug
89to cpan-RT.
90
91# SEE ALSO
92
93[PSGI](http://search.cpan.org/perldoc?PSGI)
94
95[Plack](http://search.cpan.org/perldoc?Plack)
96
97# AUTHOR
98
99Goro Fuji (gfx) <gfuji(at)cpan.org>
100
101# LICENSE AND COPYRIGHT
102
103Copyright (c) 2011, Goro Fuji (gfx). All rights reserved.
104
105This library is free software; you can redistribute it and/or modify
106it under the same terms as Perl itself. See [perlartistic](http://search.cpan.org/perldoc?perlartistic) for details.
107