#!/usr/bin/perl
use warnings;
package Animal;
sub new
{
my $class=shift;
my $self={name=>shift, sound=>shift};
return bless $self, $class;
}
sub say
{
my $self=shift;
print "Здравствуйте, я $self->{name}. $self->{sound}!\n";
}
package main;
my $cat=Animal->new('Кошка', 'Мяу');
my $dog=Animal->new('Собака', 'Гав');
my $pig=Animal->new('Свинья', 'Хрю');
my $ram=Animal->new('Баран', 'Бее');
my $cow=Animal->new('Корова', 'Муу');
my $horse=Animal->new('Лошадь', 'И-го-го');
$_->say for $cat, $dog, $pig, $ram, $cow, $horse;
Простые числа: фильтрация
#!/usr/bin/perl
use warnings;
no warnings 'recursion';
package Sequence;
sub new
{
my $n=2;
return bless \$n, shift;
}
sub get
{
my $self=shift;
return $$self++;
}
package Filter;
sub new
{
my $class=shift;
return bless {source=>shift, p=>shift}, $class;
}
sub get
{
my $self=shift;
my $n=$self->{source}->get;
return ($n % $self->{p})? $n: $self->{source}->get;
}
package main;
my $n=shift or die "Укажите число в командной строке\n";
my $sequence=Sequence->new;
while()
{
my $p=$sequence->get;
last if $p>$n;
print "$p\n";
$sequence=Filter->new($sequence, $p);
}