On Sep 29, 2007, at 12:58 PM, Robert Dober wrote: >> Perl on the other hand is pass-by-reference: >> >> $ perl -wle '$a = 0; sub { $_[0] = 1 }->($a); print $a' > hmm I am not sure about it, > > perl -e '@x=qw{a};print $x[0]; sub{ @_ = qw{b}}->(@x); print $x[0]' > > I guess the best thing one could say is > > perl simulates pass by reference by passing one array by value. > > Of course if one makes abstraction of @_... In Perl semantics when you write print $a, %a; print receives as many arguments as one plus twice the buckets in %a, which gets flattened. You are not passing %a, you pass a handful of scalars unrelated to %a from the subroutine's view. Before you call a subroutine its arguments are first evaluated in list context (except if a prototype says otherwise). Aliases (SV*s) of the resulting list of scalars are then pushed onto the argument stack and made available to the subroutine via @_. There's no array involved in the call except as a metaphor so to speak. That's considered pass-by-reference semantics and there's consensus about it in the Perl community[*]. In Perl you emulate pass-by-value with idioms like this: my ($foo, $bar) = @_; -- fxn [*] See pages 219-220 of the Camel Book, perlsub, section "Argument stack" in perlhack, chapter "Perl Internals" in the Panther Book, ....