Skip to content

Commit 914b0b5

Browse files
authored
Merge pull request #19 from cfconrad/fix_args_doku
Fix `args` processing
2 parents 0f10341 + 81b4858 commit 914b0b5

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

lib/Mojo/IOLoop/ReadWriteProcess.pm

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ has [qw(blocking_stop serialize quirkiness total_sleeptime_during_kill)] => 0;
4343
has [
4444
qw(execute code process_id pidfile return_status),
4545
qw(channel_in channel_out write_stream read_stream error_stream),
46-
qw(_internal_err _internal_return _status)
46+
qw(_internal_err _internal_return _status args)
4747
];
4848

4949
has max_kill_attempts => 5;
5050
has kill_whole_group => 0;
5151

52-
has args => sub { [] };
5352
has error => sub { Mojo::Collection->new };
5453

5554
has ioloop => sub { Mojo::IOLoop->singleton };
@@ -465,7 +464,7 @@ sub start {
465464
die "Nothing to do" unless !!$self->execute || !!$self->code;
466465

467466
my @args
468-
= $self->args
467+
= defined($self->args)
469468
? ref($self->args) eq "ARRAY"
470469
? @{$self->args}
471470
: $self->args
@@ -645,7 +644,7 @@ Mojo::IOLoop::ReadWriteProcess - Execute external programs or internal code bloc
645644
my $output = process( sub { print "Hello\n" } )->start()->wait_stop->getline;
646645
647646
# Handles seamelessy also external processes:
648-
my $process = process(execute=> '/path/to/bin' )->args(qw(foo bar baz));
647+
my $process = process(execute=> '/path/to/bin' )->args([qw(foo bar baz)]);
649648
$process->start();
650649
my $line_output = $process->getline();
651650
my $pid = $process->pid();
@@ -798,7 +797,7 @@ You do not need to specify C<code>, it is implied if no arguments is given.
798797
799798
# The process will print "Hello User"
800799
801-
Array or arrayref of options to pass by to the external binary or the code block.
800+
Arguments pass to the external binary or the code block. Use arrayref to pass many.
802801
803802
=head2 blocking_stop
804803

t/01_run.t

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,4 +540,24 @@ process';
540540
'setting MOJO_PROCESS_DEBUG to 1 enables debug mode when executing external process';
541541
};
542542

543+
subtest 'process_args' => sub {
544+
use Mojo::IOLoop::ReadWriteProcess;
545+
my $code = sub {
546+
shift;
547+
print $_.$/ for(@_);
548+
};
549+
550+
my $p = Mojo::IOLoop::ReadWriteProcess->new($code, args => '0' )->start->wait_stop();
551+
is($p->read_all_stdout(), "0$/", '1) False scalar value was given as args.');
552+
553+
$p = Mojo::IOLoop::ReadWriteProcess->new($code)->args('0')->start->wait_stop();
554+
is($p->read_all_stdout(), "0$/", '2) False scalar value was given as args.');
555+
556+
$p = Mojo::IOLoop::ReadWriteProcess->new($code, args => [(0..3)] )->start->wait_stop();
557+
is($p->read_all_stdout(), "0$/1$/2$/3$/", '1) Args given as arrayref.');
558+
559+
$p = Mojo::IOLoop::ReadWriteProcess->new($code)->args([(0..3)])->start->wait_stop();
560+
is($p->read_all_stdout(), "0$/1$/2$/3$/", '2) Args given as arrayref.');
561+
};
562+
543563
done_testing;

0 commit comments

Comments
 (0)