コンテキストよくわからぬ……

素数を作って表示するプログラム、1時間半かかった……
コンテキストぐぬぬ


Prime.pm

package Prime;
use strict;
use warnings;


sub new {
    my $class = shift;
    my $limit = shift;
    my $self = bless {
        primes => [],
    }, $class;

    $self->{primes} = [ $self->init($limit) ];  # ここが不安

    return $self;
}


# make up an array of primes
sub init {
    my $self = shift;
    my $limit = shift;

    my @primes = (2,3);
    my $count = 2;
    my $i = 6;
    while ($count < $limit) {
        my ($l, $r) = ($i-1, $i+1);
        my ($lp, $rp) = (1, 1);  # flag: (l, r) is prime or not
        foreach (@primes) {
            $lp = 0 if $l % $_== 0;
            $rp = 0 if $r % $_== 0;            
        }
        if ($lp) {
            push(@primes, $l);
            $count++;
        }
        if ($rp) {
            push(@primes, $r);
            $count++;
        }
        $i += 6;
    }
 
    return @primes;
}


# get prime at i
sub at {
    my ($self, $i) = @_;
    return $self->{primes}[$i];
}

1;

tes_prime.pl

use Prime;

my $p = Prime->new(100);

for my $i (0..99) {
    print "$i: ";
    print $p->at($i) . "\n";
}
print "done!\n";