Skip to content

Instantly share code, notes, and snippets.

@colomon
Created August 21, 2010 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colomon/542417 to your computer and use it in GitHub Desktop.
Save colomon/542417 to your computer and use it in GitHub Desktop.
class LinesIter is Iterator {
has $!filehandle;
has $!value;
method new($fh) {
my $result = self.CREATE;
pir::setattribute__vPsP($result, '$!filehandle', $fh);
$result;
}
method infinite() { False }
method reify() {
unless $!value.defined {
$!value := pir::new('Parcel');
my $line = $!filehandle.get;
if $line.defined {
pir::push($!value, $line);
$line = $!filehandle.get;
if $line.defined {
pir::push($!value, $line);
$line = $!filehandle.get;
if $line.defined {
pir::push($!value, $line);
pir::push($!value, LinesIter.new($!filehandle));
}
}
}
}
$!value;
}
}
my $count = 0;
for LinesIter.new($*IN).list {
$count++;
}
say :$count.perl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment