I'm working on a project for myself and I've been having an issue with passing a hash to a subroutine. There's several subroutines actually but I tried to strip it down to the bare minimum. I realize that the %mdc is a global hash so I don't have to pass it around to all of the subroutines I have but I'd prefer it that way out of habit. While the below snippet does work it feels like it could be cleaner.
Anyway, the part that's got my thinking that there must be a better way are the lines:
%mdc_def = mysub(\%mdc);
%mdc = %$mdc_def;
It feels... klunky to me but I'm not finding a way to make those two lines be just one... something like (even though it's bad syntax):
%mdc = %mysub(\%mdc);
Then if I should have a mysub2 that would also need that hash I'd have to do the same two line shuffle in that subroutine as well. It just feels messy.
Here's the snippet that I'm working on:
use strict;
use warnings;
my %mdc = ();
my $mdc_def;
$mdc{abc} = 123;
$mdc{cde} = 234;
foreach (sort keys (%mdc)) { print "before $_ = $mdc{$_}\n"; }
$mdc_def = mysub(\%mdc);
%mdc = %$mdc_def;
foreach (sort keys (%mdc)) { print " after $_ = $mdc{$_}\n"; }
sub mysub {
my ($mdc_def) = @_;
my %m = %$mdc_def;
$m{def} = 345;
$m{efg} = 456;
return \%m;
}
Thanks in advance.