Lifehacks

How do I return a Value in Perl?

How do I return a Value in Perl?

Perl | return() Function return() function in Perl returns Value at the end of a subroutine, block, or do function. Returned value might be scalar, array, or a hash according to the selected context.

How do I return a Value from a subroutine in Perl?

Returning a Value from a Subroutine A value can be returned from a subroutine by using the return() function. When this function is used, the subroutine executed is completed. The return value is a single value. Therefore in order to return an array or hash, create a reference first and return that value.

When do you return a value in Perl?

return() function in Perl returns Value at the end of a subroutine, block, or do function. Returned value might be scalar, array, or a hash according to the selected context. Syntax: return Value.

What happens to the empty sub in Perl?

If no return is found and if the last statement is an expression, its value is returned. If the last statement is a loop control structure like a foreach or a while, the returned value is unspecified. The empty sub returns the empty list. Aside from an experimental facility (see “Signatures” below), Perl does not have named formal parameters.

What is the context of a subroutine in Perl?

Subroutine Call Context. The context of a subroutine or statement is defined as the type of return value that is expected. This allows you to use a single function that returns different values based on what the user is expecting to receive. For example, the following localtime() returns a string when it is called in scalar context,…

What happens if there is no expr in Perl?

If no EXPR is given, returns an empty list in list context, the undefined value in scalar context, and (of course) nothing at all in void context. (In the absence of an explicit return, a subroutine, eval, or do FILE automatically returns the value of the last expression evaluated.)