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

Snippet 1:

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

Snippet 2:

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

One of the above two snippets compiles. One does not. Without testing, can you tell which is which (and what, if any, output it produces) and, more importantly, why? To be fair, I can answer the first question, but not the second (I suspect that we're simply throwing too much at the Perl parser).

I think this is a bug in Perl, but perhaps it should be one of those "don't do that" sort of bugs. Thus, this is really an unfair quiz :)

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re: qq{bad quotes} quiz
by Juerd (Abbot) on Mar 08, 2004 at 22:42 UTC

    I think this is a bug in Perl, but perhaps it should be one of those "don't do that" sort of bugs.

    Colour coding helps, in this case. To see why the one that doesn't compile doesn't compile, paste both at http://tnx.nl/scribble.plp.

    It is not a bug, but Perl 6 will fix it nevertheless. See also "Gory details of parsing quoted construct" in perlop, which explains that "Finding the end" is done before "Interpolation".

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re: qq{bad quotes} quiz
by Abigail-II (Bishop) on Mar 09, 2004 at 10:30 UTC
    It's basically the same reason why:
    m / foo # / bar /x;
    fails to do what you expect it to do. Perl will first try to find the entire 'token' before interpreting it. So, in your case, Perl first tries to find the entire string (that is, it has to determine the end of the string), before it pays attention to whether anything needs to be interpolated.

    Abigail

Re: qq{bad quotes} quiz
by tinita (Parson) on Mar 08, 2004 at 22:37 UTC
Re: qq{bad quotes} quiz
by Grygonos (Chaplain) on Mar 09, 2004 at 01:11 UTC
    It's what I thought it was...

    Grygonos