I think I broke it.
#!/usr/bin/perl use warnings; use strict; my $a = {}; $a->{b} = 'b'; my $b = 'b'; my $any_string = q|a"b'c|; if ($any_string =~ /['"]b['"]/) { print "ok\n"; } if ($any_string =~ /['"]$b['"]/) { print "ok\n"; } if ($any_string =~ /["]$a->{b}["]/) { print "not ok\n"; } if ($any_string =~ /[']$a->{b}[']/) { print "not ok either\n"; }

Replies are listed 'Best First'.
Re: Ah Crap
by JavaFan (Canon) on Jul 22, 2010 at 01:56 UTC
    Broke what?

    This thing: /["]$a->{b}["]/ doesn't do what you think. It's not a pattern matching "b". The $a->{b}["] is that start of a long expression: $a->{b} is treated as an array reference, with ["] the start of an index - the expression in the index starting with a string. A string that isn't closed, hence the error.

Re: Ah Crap
by ww (Archbishop) on Jul 22, 2010 at 01:52 UTC
    I think you need to expand your narrative to give us an explanation of why this is a mediation... or need to ask a question (and apologize for posting to the wrong section) if this reflects some puzzle you can't solve.

    My crystal ball is broken so you need to use the advice here.

      I think it's a fine meditation. Profound. Simple, yet deep. It forces us to consider things from a position of uncertainty, and recognises that we all have to confront the infinite void with open minds.

      Having said that, I'm glad not all meditations are like this.

      --
      use JAPH;
      print JAPH::asString();

Re: Ah Crap
by duelafn (Parson) on Jul 22, 2010 at 11:27 UTC

    Hm, took me a while, even though you have a very nice hint in the second example. I instinctively rewrote it to if ($any_string =~ /['"]${b}['"]/), but it took me much longer to notice/grok that the same thing was happening in the third and fourth examples. (perhaps it is too early?)... Yep, another example of how exciting Perl's string interpolation can be.

    Good Day,
        Dean

Re: Regexes that look like dereferences but aren't
by tye (Sage) on Jul 22, 2010 at 22:14 UTC

    What, nobody will offer a single suggestion for how to fix such?

    /['"]$a->{b} ['"]/x /['"]\Q$a->{b}\E['"]/ /['"]$a->{b}(?:)['"]/

    I was surprised to find that the detection of the regex terminating delimiter depends on part of the parsing of the regex contents. I'm not sure I like that.

    I was also surprised that my first attempt didn't work, since I thought this was supported for exactly this type of problem, despite the ambiguity it presents:

    /['"]${a->{b}}['"]/

    I'm curious if any of these surprises are because things have changed since some earlier version of Perl, but I don't have the time right now to find older versions to test against.

    - tye        

      This works:

      /["']$${a{b}}["']/

      Good Day,
          Dean