5

Let's say I have

my %foo;

Can I set keys foo, bar, baz to a b c by taking a slice and doing parallel assignment with postfix notation?

%foo->@{qw/foo bar baz/} = qw/a b c/

I used this syntax and I was told it was only "accidentally working". I don't see it generating a warning, and I also don't see it documented anywhere. Is this behavior supported or not?

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • 3
    Since deref syntax makes no sense on non-references, this would clearly seem to be a bug. This also breaks the expectation that `LEFT->@{RIGHT}` = `@{LEFT}{RIGHT}` – the circumfix deref would try to stringify `%foo` when substituted into that pattern. Please just use a normal `@foo{...}` slice. – amon Apr 15 '20 at 21:07
  • `@foo{qw/foo bar baz/} = qw/a b c/;` or in case of reference `@{$fooref}{qw/foo bar baz/}=qw/a b c/`. Postfix syntax is new too me and I can not comment on it's usage. – Polar Bear Apr 15 '20 at 22:58
  • The left side of `->` is supposed to be an expression that returns a reference. – ikegami Apr 16 '20 at 00:25
  • 1
    An interesting bug! No way that that should work, and should be reported. (I also find it to work on my perlbrew v5.30, no warnings.) – zdim Apr 16 '20 at 00:47
  • It won't do it for an array, `Can't use an array as a reference...` – zdim Apr 16 '20 at 00:50
  • perlbug ref https://github.com/Perl/perl5/issues/17722 – Ether Apr 16 '20 at 22:25

1 Answers1

8

The left side of -> is supposed to be an expression that returns a reference. Use anything else at your own risk.


%foo->{a} used to work.

$ 5.10t/bin/perl -e'my %foo; %foo->{a} = 1; print "ok\n";'
ok

This was deemed to be bug.

$ 5.12t/bin/perl -e'my %foo; %foo->{a} = 1; print "ok\n";'
Using a hash as a reference is deprecated at -e line 1.
ok

$ 5.20t/bin/perl -e'my %foo; %foo->{a} = 1; print "ok\n";'
Using a hash as a reference is deprecated at -e line 1.
ok

$ 5.22t/bin/perl -e'my %foo; %foo->{a} = 1; print "ok\n";'
Can't use a hash as a reference at -e line 1.

There's no reason to believe %foo->@{...} is any more valid than %foo->{...}.


Bug reported.

ikegami
  • 367,544
  • 15
  • 269
  • 518