PDA

View Full Version : Quick Perl Question



MiniStephan
November 11th, 2009, 11:32 PM
I'm starting to learn OOP Perl, and I came across this line of code when learning methods:


sub name {
my $self = shift;
if (@_) { $self->{NAME} = shift }
return $self->{NAME};
}

What's the if (@_) line used for?

myrtle1908
November 11th, 2009, 11:37 PM
I'm starting to learn OOP Perl, and I came across this line of code when learning methods:


sub name {
my $self = shift;
if (@_) { $self->{NAME} = shift }
return $self->{NAME};
}

What's the if (@_) line used for?

It's checking if parameters were passed to the subroutine. In this case, if there are parameters, it is shifting off the parameter list and setting NAME.

MiniStephan
November 11th, 2009, 11:38 PM
Oh yeah, that helps, thanks. I really should have seen that >.<