wantarray() returns true if caller wants list, false if caller wants scalar, and an undefined value if the caller wants nothing. : wantarray « Subroutine « Perl






wantarray() returns true if caller wants list, false if caller wants scalar, and an undefined value if the caller wants nothing.

   

#!/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 wantarray Function and User-Defined Subroutines