Hello! I often find myself needing to input and return multiple references from a subroutine and end up writing code that takes the following form:

my %x = ("a" => "red"); my %y = ("b" => "green"); my %z = ("c" => "black"); my ($ref1, $ref2, $ref3) = &modfifyHash(\%x, \%y, \%z); %x = %{ $ref1 }; %y = %{ $ref2 }; %z = %{ $ref3 }; sub modfifyHash { my ($ref1, $ref2, $ref3) = @_; my %x = %{ $ref1 }; my %y = %{ $ref2 }; my %z = %{ $ref3 }; $x{ "a" } = "circle"; $y{ "b" } = "square"; $z{ "c" } = "rectangle"; return(\%x, \%y, \%z) }

The, 1) create a variable to hold the reference, 2) dereference each reference, steps takes up considerable amount of space and feels like it just clutters the whole code a lot (especially when I end up doing this many times throughout a script and/or am inputing or returning many referenced variable). So I am looking for a more compact and elegant solution.... I would like to write something like:

my %x = ("a" => "red"); my %y = ("b" => "green"); my %z = ("c" => "black"); (%x, %y, %z) = %{ &modfifyHash(\%x, \%y, \%z) }; sub modfifyHash { my (%x, %y, %z) = %{ @_ }; $x{ "a" } = "circle"; $y{ "b" } = "square"; $z{ "c" } = "rectangle"; return(\%x, \%y, \%z) }

...but this does not produce the desired results, i.e. each input/returned hash in an individual variable. I also thought I might be able to use map in some clever way like:

my %x = ("a" => "red"); my %y = ("b" => "green"); my %z = ("c" => "black"); (%x, %y, %z) = map %{ $_ }, &modfifyHash(\%x, \%y, \%z); sub modfifyHash { my (%x, %y, %z) = map %{ $_ }, @_; $x{ "a" } = "circle"; $y{ "b" } = "square"; $z{ "c" } = "rectangle"; return(\%x, \%y, \%z) }

...but, again, this is not working as I would like. Could anyone share how they typically handle this in a better way?


In reply to Elegantly dereferencing multiple references by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.