in reply to Re: Problem using value of variable in a regular expression
in thread Problem using value of variable in a regular expression

moritz, Thanks for the quick reply. But, what surprises me is double-quotes eating up the backslash. This is very arbitrary, unintuitive(at least to me), and also undesirable. Do you know if this is documented anywhere? Thanks for all the help.
  • Comment on Re^2: Problem using value of variable in a regular expression

Replies are listed 'Best First'.
Re^3: Problem using value of variable in a regular expression
by ikegami (Patriarch) on Jul 30, 2010 at 14:34 UTC

    It's not undesirable at all. In double quotes, the backslash is indicates the start of an escape allowing "\n" to mean newline, "\"" to mean a double quote, etc. Double quotes are documented in perlop.

    You're just using the wrong tool.

    $failmsg = qr/\*E/;
Re^3: Problem using value of variable in a regular expression
by FunkyMonk (Bishop) on Jul 30, 2010 at 10:40 UTC
      Thanks for the reply. I wasn't aware of the interpolation in perl. The problem with searching a solution for such a query is that one has to know the name of the concept behind the issue(here it is "interpolation") to get quickly to the answer. But then if one knew the concept, he/she wouldn't be facing the problem in the first place.
        Well, interpolation of control chars is a major feature of many programming languages, not just perl. Surely you've seen something like printf("Hello, World!\n"); before?

        I agree though, finding your way around perl's extensive documentation can be a scary thought. I suggest you look at perlintro if you haven't already, and from there take some time each day to at least skim over the other perl tutorials. http://perldoc.perl.org/ is a very convenient way of reading the docs and the tutorials are listed here.

        Good luck

        The problem with searching a solution for such a query is that one has to know the name of the concept behind the issue(here it is "interpolation") to get quickly to the answer. But then if one knew the concept, he/she wouldn't be facing the problem in the first place.

        Yes, that's indeed a problem. That's why places like perlmonks, usenet and various IRC channels exist.

        One appraoch that I'm eager to try out for Perl 6 is to write a tool that, given some piece of code, points you to the applicable documentation, and also tells you which possible usage of a character it is (for example the slash can be numeric division, or it can be the start of a regex).

        In an ideal world, given the string

        my $x = "\*E"

        It would tell you that 'my' is a declarator (and point you to the docs), = is assignment (+ pointer to docs), "..." is a quote (+ pointer to docs", and \ introduces an escape sequence (+ pointer to docs).

        For Perl 6 I know how to obtain the parse tree, but the docs are too sparse to make it useful right now. For Perl 5 it's hard to get the parse tree.

        But it's on my list of long-term projects to hack on (and some of these projects done from time to time).

        Perl 6 - links to (nearly) everything that is Perl 6.

        There are a lot of concepts in any programming language that are not easily searchable. Think operator precedence, how to represent array or hash constants, what happens to the loop variable after the loop.

        For all of these concepts I'm always glad I have a perl book beside me. The index at the end of the book and especially the circumstance that all special characters are listed in it, helps tremendously.

        I don't see a reason why such an index could not be implemented into the perldoc system, but until someone makes the editorial effort a book index is the fastest way to find your way around a language you haven't been properly introduced to ;-)

Re^3: Problem using value of variable in a regular expression
by JavaFan (Canon) on Jul 30, 2010 at 10:43 UTC
    Documented in various places. perlop, sections Quote-Like Operators and Gory details of parsing quoted constructs, and perldata, section Scalar value constructors.