7

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?

4
  • 2
    Confirmed. This is truly weird. But when I ran the code on v14.2 or v16.3 it ran without issue → it seems to have been fixed, although I can't see any mention of related fixes in the perldeltas. Commented May 7, 2013 at 5:24
  • Running your second example on perl 5.16.2, I get b 1 c 2 a 4 Commented May 7, 2013 at 5:26
  • This is a bit odd. I can replicate this on one of my machines: This is perl 5, version 12, subversion 4 (v5.12.4) built for darwin-thread-multi-2level Commented May 7, 2013 at 6:24
  • the return of hash/hash element assignments in list context have always been a little wonky Commented May 7, 2013 at 10:48

1 Answer 1

2

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/"
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you all for your responses. I accept that my site needs to update its Perl version to 5.14. It is always nice to have one's sanity confirmed when the software foundations start to give way.
@RobN You mean update to at-least 5.14 (5.18 should be available in a week or-so. Although it may cause subtle bugs in your codebase to show their faces en-mass.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.