Perl | wantarray() Function Last Updated : 07 May, 2019 Summarize Comments Improve Suggest changes Share 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 | grep() 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 Like