Boa tarde. Comecei recentemente a aprender Perl. Estava a praticar com um programa simples orientado a objetos.
Esta é a minha classe
package Consultant;
sub new {
my $class = shift;
my $self = {
_firstName => shift,
_lastName=> shift,
_age=>shift,
#_dataofBirth=>shift,
_country=>shift,
_salary=>shift,
_manager=>shift,
_contract=> shift,
_experience=> shift,
};
print "Created a Consultant\n";
bless $self, $class;
return $self;
}
sub getFirstName {
my( $self ) = @_;
return $self->{_firstName};
}
sub getLastName {
my( $self ) = @_;
return $self->{_lastName};
}
sub getAge {
my( $self ) = @_;
return $self->{_age};
}
sub getCountry {
my( $self ) = @_;
return $self->{_country};
}
sub getContract {
my( $self ) = @_;
return $self->{_contract};
}
sub getSalary {
my( $self ) = @_;
return $self->{_salary};
}
sub getManager {
my( $self ) = @_;
return $self->{_manager};
}
sub getExperience {
my( $self ) = @_;
return $self->{_experience};
}
sub setFirstName {
my ( $self, $firstName ) = @_;
$self->{_firstName} = $firstName if defined($firstName);
return $self->{_firstName};
}
sub setLastName {
my ( $self, $lastName ) = @_;
$self->{_lastName} = $lastName if defined($lastName);
return $self->{_lastName};
}
sub setAge {
my ( $self, $age ) = @_;
$self->{_age} = $age if defined($age);
return $self->{_age};
}
sub setCountry {
my ( $self, $country ) = @_;
$self->{_country} = $country if defined($country);
return $self->{_country};
}
sub setSalary {
my ( $self, $salary ) = @_;
$self->{_salary} = $salary if defined($salary);
return $self->{_salary};
}
sub setManager {
my ( $self, $manager ) = @_;
$self->{_manager} = $manager if defined($manager);
return $self->{_manager};
}
sub setContract {
my ( $self, $contract ) = @_;
$self->{_contract} = $contract if defined($contract);
return $self->{_contract};
}
sub setExperience {
my ( $self, $experience ) = @_;
$self->{_experience} = $experience if defined($experience);
return $self->{_experience};
}
Ao tentar eliminar membros de um array com o ciclo
case "3" {
print "Deseja eliminar:\n";
print "1-Consultor\n";
print "2-Funcionário\n";
my $opcao3=<STDIN>;
chomp($opcao3);
switch($opcao3) {
case "1" {
print "Primeiro Nome:\n";
my $firstNameDespedir=<STDIN>;
chomp($firstNameDespedir);
print "Ultimo Nome:\n";
my $lastNameDespedir=<STDIN>;
chomp($lastNameDespedir);
for my $i (0 .. $#consultores) {
my $object=$consultores[$i];
my $firstName1= $object->getFirstName();
my $lastName1= $object->getLastName();
if(($lastName1 eq $lastNameDespedir) && ($firstName1 eq $firstNameDespedir)) {
splice @consultores, $i, 1;
}
else {
print "Continua\n";
}
}
}
}
}
O erro so ocorre se tentar eliminar um membro do array que não seja o primeiro.