This is my trying to implement $*ARGFILES for Rakudo for this week's
contribution to Perl 6.
class IO::ArgFiles {
    has $!filehandle;
    has $!filename;
    has @!filenames;

    method new(@filenames) {
        self.bless(*, filenames => @filenames || '-');
    }

    method get() {
        return unless $.filename;
        $!filehandle.get;
    }

    method lines() {
        gather while $.filename && !$!filehandle.eof {
            take $!filehandle.lines;
        }
    }

    method filename() {
        # return the current filename, or '-' if standard input
        if $!filehandle && $!filehandle.eof {
            undefine $!filehandle;
            undefine $!filename;
        }

        unless $!filehandle {
            $!filename = @!filenames.shift;
            return unless $!filename;

            $!filehandle = $!filename eq '-' ?? $*IN !! open $!filename;
        }

        $!filename;
    }
}

my $*ARGFILES = IO::ArgFiles.new(filenames => @*ARGS);
$*ARGFILES.filename.say;
$*ARGFILES.get.say;
$*ARGFILES.get.say;
for $*ARGFILES.lines -> $line {
    "$*ARGFILES.filename(): $line".say
}

say 'ok';

# vim: sw=4 ts=4 ft=perl6 expandtab

Reply via email to