Please explain what exactly the difference of $_ and @_ is in Perl.
When to use which, given by example code.
Please explain what exactly the difference of $_ and @_ is in Perl. When
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When in a subroutine, the array
@_gives the arguments passed to the given subroutine. For example:The output is
The scalar
$_is usually used as a variable within a loop. For example:The output is:
Likewise, we could have slightly rewritten the subroutine
print_emabove asor even as the more compact
The variable
$_can also be used as a “default argument” for certain functions. For example:which, of course, outputs
matched!.Take a look at
perldoc perlvarfor more information on these and Perl’s other “magic variables”.