[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

Re: [ID 19991231.001] interpolation of $_ is busted



On Fri, Dec 31, 1999 at 04:23:28PM +0100, Bart Schuller wrote:
> 
> I've found a bug in interpolation of $_ when followed by the characters [] .
> 
> $ perl5.00503 -e '$_="a"; print "${_}[]\n"'
> a[]

This is reasonable behavior.


> $ perl5.00562 -e '$_="a"; print "${_}[]\n"'
> []

This is not reasonable behavior; I can't explain it.  What happened to the
value of $_?


> Leaving off the curlies gives
> 
> $ perl5.00562 -e '$_="a"; print "$_[]\n"'
> syntax error at -e line 1, near "[]"
> Execution of -e aborted due to compilation errors.

This is reasonable too.  $_[] refers to the array @_, but it's a syntax
error because the index is missing.


> So I don't know how to follow $_ with literal square brackets other than
> concatenating two different strings.

Escape the bracket with a backslash.  In a double-quoted string, a
backslash followed by any non-word character is always interpolated as the
literal character.

print "$_\[]\n";


Ronald


References to:
Bart Schuller <schuller@lunatech.com>

[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]