Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
implement put sub/method
This is like 'say' insofar as it adds newline, but like 'print' insofar
as it has .Str semantics instead of .gist.
  • Loading branch information
TimToady committed Nov 13, 2015
1 parent 5ffd01f commit bcebf8e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/core/IO/Handle.pm
Expand Up @@ -880,6 +880,23 @@ my class IO::Handle does IO {
Bool::True
}

proto method put(|) { * }
multi method put(IO::Handle:D: str:D \x) {
nqp::printfh($!PIO,x);
nqp::printfh($!PIO, nqp::unbox_s($!nl-out));
Bool::True
}
multi method put(IO::Handle:D: Str:D \x) {
nqp::printfh($!PIO, nqp::unbox_s(x));
nqp::printfh($!PIO, nqp::unbox_s($!nl-out));
Bool::True
}
multi method put(IO::Handle:D: *@list is raw) { # is raw gives List, which is cheaper
nqp::printfh($!PIO, nqp::unbox_s(.Str)) for @list;
nqp::printfh($!PIO, nqp::unbox_s($!nl-out));
Bool::True
}

multi method say(IO::Handle:D: |) {
my Mu $args := nqp::p6argvmarray();
nqp::shift($args);
Expand Down
7 changes: 7 additions & 0 deletions src/core/IO/Socket.pm
Expand Up @@ -72,6 +72,13 @@ my role IO::Socket does IO {
True
}

method put (Str(Cool) $string) {
fail("Not connected") unless $!PIO;
nqp::printfh($!PIO, nqp::unbox_s($string));
nqp::printfh($!PIO, nqp::unbox_s("\n")); # XXX should be $!nl-out
True
}

method write(Blob:D $buf) {
fail('Socket not available') unless $!PIO;
nqp::writefh($!PIO, nqp::decont($buf));
Expand Down
1 change: 1 addition & 0 deletions src/core/Mu.pm
Expand Up @@ -332,6 +332,7 @@ Please refactor this code using the new Iterator / Seq interface.
proto method say(|) { * }
multi method say() { say(self) }
method print() { print(self) }
method put() { put(self) }
method note() { note(self) }

proto method gist(|) { * }
Expand Down
7 changes: 7 additions & 0 deletions src/core/Proc/Async.pm
Expand Up @@ -203,6 +203,13 @@ my class Proc::Async {
$p
}

method put(Proc::Async:D: \x, |c) {
X::Proc::Async::OpenForWriting.new(:method<say>, proc => self).throw if !$!w;
X::Proc::Async::MustBeStarted.new(:method<say>, proc => self).throw if !$!started;

self.print( x.join ~ "\n", |c );
}

method say(Proc::Async:D: \x, |c) {
X::Proc::Async::OpenForWriting.new(:method<say>, proc => self).throw if !$!w;
X::Proc::Async::MustBeStarted.new(:method<say>, proc => self).throw if !$!started;
Expand Down
20 changes: 20 additions & 0 deletions src/core/io_operators.pm
Expand Up @@ -52,6 +52,26 @@ multi sub say(**@args is raw) {
$out.print($str);
}

proto sub put(|) { * }
multi sub put() { $*OUT.print-nl }
multi sub put(Str:D \x) {
my $out := $*OUT;
my str $str = nqp::concat(nqp::unbox_s(x),$out.nl-out);
$out.print($str);
}
multi sub put(\x) {
my $out := $*OUT;
my str $str = nqp::concat(nqp::unbox_s(x.Str),$out.nl-out);
$out.print($str);
}
multi sub put(**@args is raw) {
my $out := $*OUT;
my str $str;
$str = nqp::concat($str,nqp::unbox_s(.Str)) for @args;
$str = nqp::concat($str,$out.nl-out);
$out.print($str);
}

proto sub note(|) { * }
multi sub note() {
my $err := $*ERR;
Expand Down

1 comment on commit bcebf8e

@cygx
Copy link
Contributor

@cygx cygx commented on bcebf8e Nov 13, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I may comment on the colour of this particular bikeshed: Personally, I'd prefer is the subs/methods were called println instead...

Please sign in to comment.