Perl | wantarray() Function Last Updated : 07 May, 2019 Comments Improve Suggest changes Like Article Like Report wantarray() function in Perl returns True if the currently executing subroutine expects to return a list value, and false if it is looking for a scalar value. Syntax: wantarray() Returns: true for list value and false for scalar values Example 1: Perl #!/usr/bin/perl -w # Subroutine to call wantarray() function sub geeks { return(wantarray() ? ("Geeks", "For", "Geeks") : 1); } # Calling the subroutine # in scalar and array context $value = geeks(); @value = geeks(); # Printing the values in both contexts print("Value in Scalar context: $value\n"); print("Value in List Context: @value"); Output: Value in Scalar context: 1 Value in List Context: Geeks For Geeks Example 2: Perl #!/usr/bin/perl -w # Subroutine to call wantarray() function sub geeks { if(wantarray()) { # Addition of two numbers when # wantarray() function is called # in list context $c = $a + $b; } else { # When wantarray() is called # in Scalar context return 1; } } # Driver Code $a = 10; $b = 20; $c = 0; # Calling Subroutine in scalar and list contexts $value = geeks($a, $b); @value = geeks($a, $b); # Printing values in both the contexts print("Value when called in Scalar context: $value\n"); print("Value when called in List Context: @value"); Output: Value when called in Scalar context: 1 Value when called in List Context: 30 Comment More infoAdvertise with us Next Article Perl | wantarray() Function C Code_Mech Follow Improve Article Tags : Perl Perl-function Perl-Array-Functions Similar Reads Perl | unshift() Function unshift() function in Perl places the given list of elements at the beginning of an array. Thereby shifting all the values in the array by right. Multiple values can be unshift using this operation. This function returns the number of new elements in an array. Syntax: unshift(Array, List)Returns: Nu 2 min read Perl | shift() Function shift() function in Perl returns the first value in an array, removing it and shifting the elements of the array list to the left by one. Shift operation removes the value like pop but is taken from the start of the array instead of the end as in pop. This function returns undef if the array is empt 2 min read Perl | values() Function values() Function in Perl returns the list of all the values stored in a Hash. In a scalar context it returns the number of elements stored in the Hash. Note: Values returned from the value() Function may not always be in the same order. Syntax: values Hash Returns: list of values in the list contex 2 min read Perl | push() Function push() function in Perl is used to push a list of values onto the end of the array. push() function is often used with pop to implement stacks. push() function doesn't depend on the type of values passed as list. These values can be alpha-numeric. Syntax: push(Array, List) Parameter: Array: in which 2 min read Perl | grep() Function The grep() function in Perl used to extract any element from the given array which evaluates the true value for the given regular expression. Syntax: grep(Expression, @Array) Parameters: Expression : It is the regular expression which is used to run on each elements of the given array.@Array : It is 2 min read Perl | Array pop() Function pop() function in Perl returns the last element of Array passed to it as an argument, removing that value from the array. Note that the value passed to it must explicitly be an array, not a list. Syntax: pop(Array) Returns: undef if list is empty else last element from the array. Example 1: perl #!/ 1 min read Perl | exists() Function The exists() function in Perl is used to check whether an element in an given array or hash exists or not. This function returns 1 if the desired element is present in the given array or hash else returns 0. Syntax: exists(Expression) Parameters: Expression : This expression is either array or hash 2 min read Perl | splice() - The Versatile Function In Perl, the splice() function is used to remove and return a certain number of elements from an array. A list of elements can be inserted in place of the removed elements. Syntax: splice(@array, offset, length, replacement_list) Parameters: @array - The array in consideration. offset - Offset of re 9 min read Perl | Useful Array functions In Perl, array is a special type of variable. The array is used to store the list of values and each object of the list is termed as an element. Elements can either be a number, string, or any type of scalar data including another variable. Array in Perl provides various inbuilt functions to perform 2 min read Perl | print operator print operator in Perl is used to print the values of the expressions in a List passed to it as an argument. Print operator prints whatever is passed to it as an argument whether it be a string, a number, a variable or anything. Double-quotes("") are used as a delimiter to this operator. Syntax: pri 2 min read Like