Skip to content

Commit

Permalink
Implement $*ARGFILES, and make it the default handle for lines()
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Lenz <moritz@faui2k3.org>
  • Loading branch information
Tyler Curtis authored and moritz committed Jun 29, 2010
1 parent d18b5e7 commit 68852e4
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ U: tene
D: Minor Rakudo patches
E: tene@allalone.org

N: Tyler Curtis
U: tcurtis
D: $*ARGFILES
E: tyler.l.curtis@gmail.com

N: Ujwal Reddy Malipeddi
E: ujwalic@gmail.com
D: Rakudo patch
Expand Down
1 change: 1 addition & 0 deletions build/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ CORE_SOURCES = \
src/core/Hash.pm \
src/core/Enum.pm \
src/core/IO.pm \
src/core/IO/ArgFiles.pm \
src/core/IO/Socket.pm \
src/core/IO/Socket/INET.pm \
src/core/Parameter.pm \
Expand Down
2 changes: 1 addition & 1 deletion src/core/IO.pm
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class IO is Cool {
}
}

multi sub lines(IO $filehandle,
multi sub lines(IO $filehandle = $*ARGFILES,
:$bin = False,
:$enc = 'Unicode',
:$nl = "\n",
Expand Down
60 changes: 60 additions & 0 deletions src/core/IO/ArgFiles.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class IO::ArgFiles {
has $!filenames;
has $!filename;
has $!current_file;
has $!ins;

method new($filenames) {
if $filenames.elems {
self.bless(*, :filenames($filenames));
} else {
push $filenames, "-";
self.bless(*, :filenames($filenames));
}
}

method eof() {
$.next_file;
$!current_file.eof && $!filenames.elems == 0;
}

method getc() {
...
}

method get() {
$.next_file;
$!ins++;
$!current_file.get;
}

method lines() {
gather while !$.eof {
my $line = $.get;
take $line if defined $line;
}
}

method filename() {
$!filename;
}

method ins() {
$!ins;
}

method next_file() {
if (!defined $!current_file) || ($!current_file.eof) {
$!current_file.close if $!current_file && $!filename ne '-';
fail if $!filenames.elems == 0;
$!filename = $!filenames.shift;
$!current_file = $!filename eq '-' ?? $*IN !! open($!filename);
}
}
}

sub ARGFILES_CREATE() {
IO::ArgFiles.new(@*ARGS);
}

# vim: ft=perl6
3 changes: 3 additions & 0 deletions src/glue/run.pir
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ of the compilation unit.
set_hll_global '@ARGS', $P2
setprop $P2, "rw", true

$P3 = 'ARGFILES_CREATE'()
set_hll_global '$ARGFILES', $P3

## set up %*VM
load_bytecode 'config.pbc'
.local pmc vm, interp, config
Expand Down

0 comments on commit 68852e4

Please sign in to comment.