Can’t call method "getFirstName" on an Undefined value at Consmanagement.pl

Asked

Viewed 15 times

0

Good afternoon. I recently started learning Perl. I was practicing with a simple object-oriented program.

This is my class

 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};
}

When trying to delete members from an array with the cycle

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";
                                }

                            }
                        }
                    }
                }

The error only occurs if you try to delete a member of the array other than the first one.

1 answer

0

It’s settled. What was happening was that the iterator prematurely detected an equality and eliminated, after that, it was not possible to call the method. Solved by adding a last; inside the string equality if.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.