John Rambo John Rambo - 2 years ago 148
Perl Question

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

I am new to Perl language.

Can anyone explain the following subroutine, what is the difference between

my ($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};

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download