0

There's any difference, concerning to memory and/or disc usage, between these two ways of passing variables to a subroutine:

&subrutine_1($hash_reference);

sub subrutine
{
    my $hash = $_[0];
    my $var_1 = $$hash{'var_1'};
    my $var_2 = $$hash{'var_2'};
    my $var_3 = $$hash{'var_3'};
}

or

&subrutine_1($hash_reference);

sub subrutine
{
    my $var_1 = $_[0]{'var_1'};
    my $var_2 = $_[0]{'var_2'};
    my $var_3 = $_[0]{'var_3'};
}

Thanks!

1 Answer 1

1

Disk usage should be the same, as there's no I/O operation. Memory consumption will be larger in the first case, because you need one more scalar variable $hash. It will only store a reference, so the difference is minimal.

Really copying the hash can consume a lot more memory, though:

sub subroutine {
    my %hash = %{ $_[0] };
    my $var_1 = $hash{var_1};
    # ...
}
Sign up to request clarification or add additional context in comments.

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.