diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-07-24 13:41:28 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-07-24 13:41:28 +0300 |
commit | 73365dbc9cda550963df506ea739cbcd73817106 (patch) | |
tree | 50c0ba403bcf4fcdd6aa3e142c04142ce734d009 | |
parent | 1718b7e87b3ce2d588f2412ac41bea6e7048709f (diff) | |
download | posixruncapture-73365dbc9cda550963df506ea739cbcd73817106.tar.gz posixruncapture-73365dbc9cda550963df506ea739cbcd73817106.tar.bz2 |
Update docs
-rw-r--r-- | Capture.xs | 23 | ||||
-rw-r--r-- | lib/POSIX/Run/Capture.pm | 206 |
2 files changed, 205 insertions, 24 deletions
@@ -3,18 +3,6 @@ | |||
3 | MODULE = POSIX::Run::Capture PACKAGE = POSIX::Run::Capture PREFIX = capture_ | 3 | MODULE = POSIX::Run::Capture PACKAGE = POSIX::Run::Capture PREFIX = capture_ |
4 | PROTOTYPES: ENABLE | 4 | PROTOTYPES: ENABLE |
5 | 5 | ||
6 | =head2 new POSIX::Run::Capture | ||
7 | |||
8 | new POSIX::Run::Capture([ $command, @args ]); | ||
9 | |||
10 | new POSIX::Run::Capture(argv => [ $command, @args ], | ||
11 | stdout => sub { ... }, | ||
12 | stderr => sub { ... }, | ||
13 | timeout => N) | ||
14 | |||
15 | |||
16 | =cut | ||
17 | |||
18 | POSIX::Run::Capture | 6 | POSIX::Run::Capture |
19 | capture_new(package, ...) | 7 | capture_new(package, ...) |
20 | char *package; | 8 | char *package; |
@@ -66,7 +54,8 @@ capture_new(package, ...) | |||
66 | croak("program argument is not a scalar"); | 54 | croak("program argument is not a scalar"); |
67 | else | 55 | else |
68 | prog = val; | 56 | prog = val; |
69 | } else if (strcmp(kw, "input") == 0) { | 57 | } else if (strcmp(kw, "input") == 0 |
58 | || strcmp(kw, "stdin") == 0) { | ||
70 | input = val; | 59 | input = val; |
71 | } else | 60 | } else |
72 | croak("unknown keyword argument %s", kw); | 61 | croak("unknown keyword argument %s", kw); |
@@ -80,6 +69,14 @@ void | |||
80 | capture_DESTROY(cp) | 69 | capture_DESTROY(cp) |
81 | POSIX::Run::Capture cp; | 70 | POSIX::Run::Capture cp; |
82 | 71 | ||
72 | =head2 $obj->set_argv_ref($aref) | ||
73 | |||
74 | Sets command argument vector. The B<$aref> parameter is an array reference. | ||
75 | |||
76 | This is an auxiliary method. Use B<set_argv> instead. | ||
77 | |||
78 | =cut | ||
79 | |||
83 | void | 80 | void |
84 | capture_set_argv_ref(cp, argv) | 81 | capture_set_argv_ref(cp, argv) |
85 | POSIX::Run::Capture cp; | 82 | POSIX::Run::Capture cp; |
diff --git a/lib/POSIX/Run/Capture.pm b/lib/POSIX/Run/Capture.pm index dacdcfd..5dedad0 100644 --- a/lib/POSIX/Run/Capture.pm +++ b/lib/POSIX/Run/Capture.pm | |||
@@ -64,27 +64,211 @@ POSIX::Run::Capture - run command and capture its output | |||
64 | =head1 SYNOPSIS | 64 | =head1 SYNOPSIS |
65 | 65 | ||
66 | use POSIX::Run::Capture; | 66 | use POSIX::Run::Capture; |
67 | |||
68 | 67 | ||
68 | $obj = new POSIX::Run::Capture(argv => [ $command, @args ], | ||
69 | program => $prog, | ||
70 | stdin => $fh_or_string, | ||
71 | stdout => sub { ... }, | ||
72 | stderr => sub { ... }, | ||
73 | timeout => $n); | ||
74 | $obj->run; | ||
75 | |||
76 | $num = $obj->errno; | ||
77 | $num = $obj->status; | ||
78 | $num = $obj->length($chan); | ||
79 | $num = $obj->nlines($chan); | ||
80 | |||
81 | $str = $obj->next_line($chan); | ||
82 | $aref = $obj->get_lines($chan); | ||
83 | $obj->rewind($chan) | ||
84 | |||
85 | $obj->set_program($prog); | ||
86 | $obj->set_timeout($n); | ||
87 | $obj->set_input($fh_or_string); | ||
88 | |||
89 | $aref = head3 $obj->argv; | ||
90 | $str = =head3 $obj->program | ||
91 | $num = $obj->timeout; | ||
92 | |||
69 | =head1 DESCRIPTION | 93 | =head1 DESCRIPTION |
70 | 94 | ||
71 | The API is in development. | 95 | This module prefers performance and effectiveness over portability. As its |
96 | name suggests, it can be used only on POSIX systems. | ||
97 | |||
98 | =head2 new POSIX::Run::Capture | ||
99 | |||
100 | Creates a new capture object. There are three possible invocation modes. | ||
101 | |||
102 | new POSIX::Run::Capture(argv => [ $command, @args ], | ||
103 | program => $prog, | ||
104 | stdin => $fh_or_string, | ||
105 | stdout => sub { ... }, | ||
106 | stderr => sub { ... }, | ||
107 | timeout => $n) | ||
108 | |||
109 | When named arguments are used, the following keywords are allowed: | ||
110 | |||
111 | =over 4 | ||
112 | |||
113 | =item B<argv> | ||
114 | |||
115 | Defines the command (B<C argv[0]>) and its arguments. In the absense of | ||
116 | B<program> argument, B<$argv[0]> will be run. | ||
117 | |||
118 | =item B<program> | ||
119 | |||
120 | Sets the pathname of binary file to run. | ||
121 | |||
122 | =item B<stdin> or B<input> | ||
123 | |||
124 | Supplies standard input for the command. The argument can be a string or | ||
125 | a file handle. | ||
126 | |||
127 | =item B<stdout> | ||
128 | |||
129 | Sets the I<line monitor> function for standard output. Line monitor is | ||
130 | invoked each time a complete line is read, or the EOF is hit on the standard | ||
131 | output. The acquired line is passed to the monitor as its argument. The | ||
132 | following example monitor function prints its argument to STDOUT: | ||
133 | |||
134 | sub stdout_monitor { | ||
135 | my $line = shift; | ||
136 | print $line; | ||
137 | } | ||
138 | |||
139 | Notice that the last line read can lack the teminating newline character. | ||
140 | |||
141 | =item B<stderr> | ||
142 | |||
143 | Sets the I<line monitor> function for standard error stream. See the | ||
144 | description above. | ||
145 | |||
146 | =item B<timeout> | ||
147 | |||
148 | Sets execution timeout, in seconds. If the program takes longer than B<$n> | ||
149 | seconds to terminate, it will be forcibly terminated (by sending the B<SIGKILL> | ||
150 | signal). | ||
151 | |||
152 | =back | ||
72 | 153 | ||
73 | =head2 EXPORT | 154 | =head3 new POSIX::Run::Capture([ $command, @args ]); |
155 | |||
156 | A simplified way of creating the object, equivalent to | ||
74 | 157 | ||
75 | None by default. Use B<:std> to export the B<STDOUT> and B<STDERR> constants. | 158 | new POSIX::Run::Capture(argv => [ $command, @args ]); |
159 | |||
160 | =head3 new POSIX::Run::Capture; | ||
161 | |||
162 | Crates an empty capture object. | ||
163 | |||
164 | Whatever constructor is used, the necessary parameters can be set | ||
165 | or changed later, using B<set_argv>, B<set_program>, B<set_input>, | ||
166 | and B<set_timeout>. | ||
167 | |||
168 | Monitors can be defined only when creating the object. | ||
169 | |||
170 | =head2 Modifying the object. | ||
171 | |||
172 | The following methods modify the object: | ||
76 | 173 | ||
174 | =head3 $obj->set_program($prog) | ||
175 | |||
176 | Sets the pathname of the command to run. | ||
77 | 177 | ||
78 | =head1 SEE ALSO | 178 | =head3 $obj->set_timeout($n) |
179 | |||
180 | Sets runtime timeout, in seconds. | ||
181 | |||
182 | =head3 $obj->set_input($fh_or_string) | ||
183 | |||
184 | Sets standard input for the program. Argument must be either a filehandle | ||
185 | open for reading or a string. The filehandle will be repositioned to its | ||
186 | beginning prior to use. | ||
187 | |||
188 | =head2 Accessors | ||
189 | |||
190 | The following accessors return parameters associated with the object: | ||
191 | |||
192 | =head3 $obj->argv | ||
193 | |||
194 | Returns a reference to the B<argv> array associated with the object. | ||
195 | |||
196 | =head3 $obj->program | ||
197 | |||
198 | Returns the pathname of the executable program. | ||
199 | |||
200 | =head3 $obj->timeout | ||
201 | |||
202 | Returns the runtime timeout or B<0> if no timeout is set. | ||
203 | |||
204 | =head2 Running the command | ||
205 | |||
206 | =head3 $obj->run | ||
207 | |||
208 | Runs the program. Returns B<1> on successful termination, B<0> otherwise. | ||
209 | |||
210 | =head3 $obj->errno | ||
211 | |||
212 | If the last call to B<run> returned false, this method returns the | ||
213 | value of the system error number (the C B<errno> variable). | ||
214 | |||
215 | Upon successful return from B<$obj-E<gt>run>, the following accessors can | ||
216 | be used: | ||
217 | |||
218 | =head3 $obj->status | ||
219 | |||
220 | Returns the termination status of the program. Use the macros from | ||
221 | B<POSIX :sys_wait_h> to analyze it. E.g.: | ||
222 | |||
223 | use POSIX qw(:sys_wait_h); | ||
224 | if ($obj->run()) { | ||