Perl: what is my ($ auto, $ firstName)

advertisements

I am new to Perl language.

Can anyone explain the following subroutine, what is the difference between my ($self, $firstName) and $self->{_firstName}, it would be helpful if you can explain by relating it with Java:

sub setFirstName {
    my ( $self, $firstName ) = @_;
    $self->{_firstName} = $firstName if defined($firstName);
    return $self->{_firstName};
}


my ( $self, $firstName ) = defines two variables and assigns them values from the list on the right hand side of the assignment operator.

$self->{_firstName} = treats the value of $self as a hashref and assigns a value to the _firstName key of the hash that it references.