in reply to Re^2: Sub hash param by reference
in thread Sub hash param by reference
You need to use the sub keyword when declaring/defining subroutines. Here's a working version based on your code.
use strict; use warnings; my (%hash); $hash{'id1'} = "1"; doSub1 (\%hash); # the return id2 should be changed within doSub2 when doSub1 call it. print "$hash{'id2'}\n"; sub doSub1 { # accept param as hash by reference my ($hrec_ref) = @_; $$hrec_ref{'id2'} = "0"; # doSub2(\$hrec_ref{'id2'}); Return Error. Not valid. (works for me - +Hippo) doSub2 (\$hrec_ref->{'id2'}); } sub doSub2 { # handle only a single string param by reference my ($href) = @_; $$href = "value changed in doSub2"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Sub hash param by reference
by hankcoder (Scribe) on Jul 10, 2016 at 12:44 UTC |