$text =~ s/(cat|tomatoes)/ ${{ qw<tomatoes cat cat tomatoes> }}{$1} /ge;
And I can't replace ${{ qw<tomatoes cat cat tomatoes> }}{$1} with { qw<tomatoes cat cat tomatoes> }->{$1},why?
UPDATE
5 @array = qw<a b c d>;
6 $ref = \@array;
7 @{$ref} = qw<1 2 3 4>;
8 #@$ref = qw<1 2 3 4>;//also works
9 print "@array";
So it indicates neither {} nor ${} is required to dereferencing,the {} is only required when ambiguity arises,and $ only in scalar context.
${{ qw<tomatoes cat cat tomatoes> }}{$1}
is
my $ref = { qw<tomatoes cat cat tomatoes> };
${ $ref }{$key}
The inner brackets form an anonymous hash constructor. It creates a hash, assigns the contents of the brackets to it, then returns a reference to it.
The outer brackets are part of the hash dereference. They can be omitted (e.g. $$ref{$key} instead of ${$ref}{$key}) when unambiguous (e.g. when dereferencing a simple scalar), but this is not such a circumstance.
One can also dereference using the arrow notation, so one could also have used
{ qw<tomatoes cat cat tomatoes> }->{$1}
The difference is that the version being used is simply a variable lookup, so it doesn't require /e, while the latter is Perl code, so it does require /e.
If you had just
${ qw<tomatoes cat cat tomatoes> }{$1}
that would be the same as
${ "tomatoes" }{$1}
since qw() in scalar context returns the last value. That, in turn, is the same as
$tomatoes{$1}
(except that use strict; wouldn't allow it) and that's obviously not what you want.
The outer brackets dereference the anonymous hash created by the inner brackets.
Update for clarification: The second format you use would work if you give the compiler a clue by putting a + in front of it:
+{ qw<tomatoes cat cat tomatoes }->{$1}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With