Hi, may I know how I can pass only specific/part of hash to another sub as reference?

sub main { my (%hash_rec) = (); $hash_rec{'id'} = "001"; &doSub1(\%hash_rec); # return result should consist of id=001 and id2=002 print "back main, id=$hash_rec{'id'}, id2=$hash_rec{'id2'}\n"; } sub doSub1 { my ($hash_ref) = @_; print "inside doSub1, id=$$hash_ref{'id'}\n"; $$hash_ref{'id2'} = "002"; # How do I call doSub2 and pass only specific hash id2 as reference +? &doSub2($hash_ref{'id2'}); } sub doSub2 { my ($id2_ref) = @_; print "inside doSub2, id2=$$id2_ref\n"; }

Inside doSub1, I would like to call another sub &doSub2($hash_ref{'id2'}); by passing only specific hash value $$hash_ref{'id2'} or part of $hash_ref as by reference. Is this possible?

Currently, I can only think of doing like codes below;

Before calling &doSub2, I assign $$hash_ref{'id2'} to another local var. Then pass that var to &doSub2 as reference. After that will replace back to $$hash_ref{'id2'}. I am thinking could there be a better or shortcut way in doing so?

my $id2_value = $$hash_ref{'id2'}; &doSub2{\$id2_value); $$hash_ref{'id2'} = $id2_value;

In reply to Sub hash param by reference by hankcoder

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.