Returning arrays from subroutines : Return « Subroutine « Perl






Returning arrays from subroutines

   


#!/usr/bin/perl -w

# Checks for desired return type.

@ar = get_value();

print "Wanted array.  Got back: @ar\n";

$v = get_value();

print "Wanted scalar. Got back: $v\n";

sub get_value {

    my(@array) = (1, 2, 3);
    my($val)   = 55;
    
    if (wantarray) {
        return @array;
    } else {
        return $val;
    }
    
}

   
    
    
  








Related examples in the same category

1.The return values of the ref function: hash
2.Return reference from a function
3.Return reference to variable
4.Return two array references from a subroutine
5.Return value from subroutine reference
6.A subroutine that returns a scalar or a list.
7.A subroutine that returns a value
8.Return Value
9.Return a reference from a sub
10.Return a subroutine from a subroutine
11.Return hash value from subroutine
12.Return more than one value from subroutine
13.Return two arrays from subroutine
14.Return value from subroutine without using the return statement
15.Returning data from subroutines
16.The last statement is the value to return
17.Using return statement
18.Using the return statement.