Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl assignment to hash in subroutine return gets weird

Running perl 5.12.4 Am getting disparity between result of a function when a hash is assigned within the return statement or beforehand. Easiest example is:

perl -e 'sub s1 {
  my @a=qw/b 1 c 2 a 3 a 4/;
  my %h=@a;
  return %h
  }
  print "@{[ s1()]}\n"'
c 2 a 4 b 1


perl -e 'sub s1 {
  my @a=qw/b 1 c 2 a 3 a 4/;
  my %h=@a;
  return %h=@a
  }
  print "@{[ s1()]}\n"'
c 2 c 2 a c

Why does (re)assigning to hash in return statement (2nd example) corrupt the returned hash?

like image 221
Rob N Avatar asked May 07 '13 04:05

Rob N


1 Answers

Because of a bug. It appears to have been fixed in 5.14.0. (Present in 5.12.4. Not present in 5.14.0)

Minimal test case:

perl -E"say %h = qw/b 1 c 2 a 3 a 4/"
like image 51
ikegami Avatar answered Oct 21 '22 11:10

ikegami