qq{Bad Quotes}

Ovid on 2004-03-08T22:16:03

This compiles:

sub Foo::show { print 'Foo::show' };
my %class = ( name => 'Foo' );
print qq{ $class{name}->show; #};

This does not:

sub Foo::show { print 'Foo::show' };
my %class = ( 'name}' => 'Foo' );
print qq{ $class{'name}'}->show; #};

I discovered this little curiosity because some programmer is using curly braces with qq// in our code:

return qq{$options{title}};


Documented behaviour

Juerd on 2004-03-08T22:46:26

See perlop, "Gory details of parsing quoted constructs".

Re:Documented behaviour

Ovid on 2004-03-08T22:55:56

I wasn't aware that it was documented until I saw your post on Perlmonks. "Find the end first" certainly clears that up.

Escape it

bart on 2004-03-08T22:49:38

Simply adding a backslash in front of the embedded "}" character makes it work:
sub Foo::show { print 'Foo::show' };
my %class = ( 'name}' => 'Foo' );
print qq{ $class{'name\}'}->show; #};
Result:
Foo->show; #

Re:Escape it

bart on 2004-03-09T12:35:28

I forgot to add: escaping works for the single quoting q{} too.
print q{ $class{'name\}'}->show; #};
result:
$class{'name}'}->show; #
In this case, backslashes work as an escape character only for the closing delimiter, its pairing delimiter, and other backslashes.
print q{\'\$\@\{\}\[\]\(\)\"\\};
result:
\'\$\@{}\[\]\(\)\"\