Using ref function to check the parameter type : ref « Data Type « Perl






Using ref function to check the parameter type

    

@a = (1, 2, 3);
@b = (4, 5, 6);

sub addem
{
    my ($ref1, $ref2) = @_;

    if (ref($ref1) eq "ARRAY" && ref($ref2) eq "ARRAY") {

        while (@$ref1) {

            unshift @result, pop(@$ref1) + pop(@$ref2);

        }

        return @result;

    } elsif (ref($ref1) eq "SCALAR" && ref($ref2) eq "SCALAR") {

        return $$ref1 + $$ref2;

    }
}

@array = addem (\@a, \@b);

print join (', ', @array);

   
    
    
    
  








Related examples in the same category

1.Return Values from the ref Function
2.Using ref function on a subroutine reference
3.Using ref function to get the type of the reference
4.Using ref inside a function
5.The return values of the ref function: ref( \\@array )
6.The return values of the ref function: function
7.The return values of the ref function: ref( \*hash )
8.The return values of the ref function: 10
9.The return values of the ref function: array