20

What's the best way to send multiple arrays, variables, hashes to a subroutine?

Simple form, works.

my $msg = &getMsg(1,2,3);
print $msg;

sub getMsg {
    my($a, $b, $c) = @_;
}

I am having difficulty with this version and am not sure how to send the data safely to the subroutine without using a global which is not what I want to do.

my @array = ('a','b','c');
my $str = "Hello";
my %hash = (
    'a' => ['100','nuts'],
    'b' => ['200','bolts'],
    'c' => ['300','screws'],
);

my $msg = getMsg(@array, $str, %hash);
print $msg;

sub getMsg {
    my (@a, $s, %h) = @_;
    my $MSG;
    foreach my $x (@a) {
        $MSG .= "\n$str, $x your hash value = $h{$x}[0] $h{$x}[1]";
    }
    return $MSG
}

2 Answers 2

25

You can use references:

getMsg(\@array, \%hash, $scalar);

sub getMsg {
    my ($aref, $href, $foo) = @_;
    for my $elem (@$aref) {
        ...
    }
}

Note that the assignment you tried:

my (@a, $s, %h) = @_;

Does not work, because @a -- being an array -- will slurp up the entire list, leaving $s and %h uninitialized.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that the passed hashref needs to be dereferenced when used in the sub $MSG.= "\n$str, $x your hash value = $h->{$x}->[0] $h->{$x}->[1]";
6

I prefer TLP's answer, but you can also use a prototype:

getMsg(@array, %hash, $scalar);

sub getMsg (\@\%$) {
    my ($aref, $href, $foo) = @_;
    for my $elem (@$aref) {
        ...
    }
}

The prototype (\@\%$) coerces the arguments to the subroutine call to a list reference, a hash reference, and a scalar before the arguments are flattened and loaded into @_. Inside the subroutine, you receive a list reference and a hash reference instead of an array and a hash.

Usually, though, don't use prototypes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.