Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
An initial implementation of $*ARGFILES and lines().
  • Loading branch information
pmichaud committed Jul 25, 2011
1 parent fedd5b3 commit ed26e45
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/core/IO.pm
Expand Up @@ -70,10 +70,15 @@ class IO {
}
}

method opened() {
nqp::p6bool(nqp::istrue($!PIO));
}

method print(*@list) {
$!PIO.print(nqp::unbox_s(@list.shift.Str)) while @list.gimme(1);
Bool::True
}

method say(|$) {
my Mu $args := pir::perl6_current_args_rpa__P();
nqp::shift($args);
Expand All @@ -94,6 +99,11 @@ multi sub open($filename, :$r, :$w, :$a, :$bin, :$chomp = Bool::True) {
IO.new.open($filename, :$r, :$w, :$a, :$bin, :$chomp);
}

proto sub lines(|$) { * }
multi sub lines($fh = $*ARGFILES, $limit = $Inf) {
$fh.lines($limit)
}

$PROCESS::IN = open('-');
$PROCESS::OUT = open('-', :w);
$PROCESS::ERR = IO.new;
Expand Down
36 changes: 36 additions & 0 deletions src/core/IO/ArgFiles.pm
@@ -0,0 +1,36 @@
my class ArgFiles {
has $.args;
has $.filename;
has $!io;
has $!ins;

method eof() { ! $!args }

method get() {
unless $!io.defined && $!io.opened {
$!filename = $!args ?? $!args.shift !! '-';
$!io = open($!filename, :r) ||
fail "Unable to open file '$!filename'";
}
my $x = $!io.get;
while !$x.defined {
$!io.close;
$!io = IO;
fail "End of argfiles reached" unless $!args;
$x = self.get;
}
$!ins++;
$x;
}

method lines($limit = *) {
my $l = $limit ~~ Whatever ?? $Inf !! $limit;
gather while !$.eof && $l-- > 0 {
my $line = $.get;
if $line.defined {
take $line;
}
}
}
}

1 change: 1 addition & 0 deletions src/core/terms.pm
Expand Up @@ -7,5 +7,6 @@ sub term:<time>() { nqp::p6box_i(pir::time__I()) }
nqp::shift($argiter) if $argiter;
@ARGS.push(nqp::p6box_s(nqp::shift($argiter))) while $argiter;
nqp::bindkey(pir::get_who__PP(PROCESS), '@ARGS', @ARGS);
$PROCESS::ARGFILES = ArgFiles.new(:args(@ARGS));
}

1 change: 1 addition & 0 deletions tools/build/Makefile.in
Expand Up @@ -173,6 +173,7 @@ CORE_SOURCES = \
src/core/Sub.pm \
src/core/Method.pm \
src/core/IO.pm \
src/core/IO/ArgFiles.pm \
src/core/Rat.pm \
src/core/Complex.pm \
src/core/Exception.pm \
Expand Down

0 comments on commit ed26e45

Please sign in to comment.