jrw has asked for the wisdom of the Perl Monks concerning the following question:

Is it anywhere documented what syntax is allowed in the index expression of a hash or array element interpolated into a string? I haven't been able to find any explicit documentation, but from playing around it appears that many (but not all) expressions which are allowed outside of interpolation are also allowed in an interpolation. For example,
   qq< $h{  fn("{") =~ /c/ } >
is allowed but
   qq< $h->{  fn("{") =~ /c/ } >
is not.

Replies are listed 'Best First'.
Re: Syntax allowed in interpolated hash/array indexes
by Somni (Friar) on Dec 10, 2007 at 23:12 UTC
    The reason your second qq is a syntax error is because you are using your end delimiter in your string.

    Try: qq[ $h->{  fn("{") =~ /c/ } ]

    The syntax allowed there is an expression, any expression will do. You are still somewhat limited, even in the expression, by the delimiters you use for the qq, however.

    There's even a module (Interpolation) that takes advantage of this by tying a hash that would simply return the result of the expression.

Re: Syntax allowed in interpolated hash/array indexes
by FunkyMonk (Bishop) on Dec 10, 2007 at 23:15 UTC
    You've just picked the wrong pair of delimeters. If you use (eg) [...] it's ok:
    qq[ $h->{ fn("{") =~ /c/ } ]

    qq will quote the first pair of matching delimeters. Which, in your case will be qq< $h->. Obviously, not what you want.