How could I pass a hash by reference to a subroutine without using the \
character in the subroutine calling expression like my_subroutine(%my_hash)
?
More explanation: (in case the previous single-line question wasn't descriptive enough)
Passing a variable by reference to a subroutine without using the \
character in subroutine calling expression like my_subroutine($my_var)
can be achieved by defining the subroutine as so:
sub my_subroutine {
my $var_ref = \shift;
...
}
I have tried the same approach with hashes but it doesn't work properly:
sub my_subroutine {
my $hash_ref = \shift;
...
}
I think that's because perl fragments any passed in hashes' keys-value pairs in a list (one-dimensional array) which is @_
, also the same is done with passed-in arrays (but for values only not with keys).
I'm looking for a workaround on this to make my_subroutine(%my_hash)
pass hash by-reference without the need to precede my subroutine parameters (hashes) by backslash character \
every time I call the subroutine. This will be helpful to make my main code look neater and leave the untidy looking to the inside of subroutines. Also if I'm working with a large group of developers, someone may forget to add the referencing character. I need to set that (hashes are always passed by reference to my_subroutine) inside the subroutine not by the other developer who calls my_subroutine.