in reply to What it mathches`

I had to fight with this a lot to get a "true" output. And even then, I cheated. The most basic of the problems is that ^ is a zero-width assertion. Think about another zero-width assertion, \b. That is, the break between alphanumeric and non-alphanumeric. If you have /a\bc/, this can never match anything because there is not, by definition, a change between word and non-word between an a and a c. Can't happen. Similarly, ^ is a zero-width assertion that asserts this is the beginning of a line. Some pedants may point out that it actually is the beginning of the string, but that's not quite true. The m modifier allows ^ to match anywhere in the string - in fact, according to perlre, the m modifier is merely removing the optimisation that perl has that assumes there is only one line in the string you're testing. That means that it's assuming it's a single line, thus ^ is the beginning of the string because of the assumption there is only one line.

Anyway, ^, being zero-width, must be right after either the physical beginning of the string, or right after a \n. It can't be right after a quote.

However, if we insert a \n into your regex right before the ^, we still don't quite get it to work because you're missing the m modifier. I'm also assuming you haven't set the deprecated $* variable (see perlvar, but don't use it - it's deprecated). Let's say we use the m modifier. It still doesn't work because $_=<> will only drag in a single line. Typing in "\nabc... won't match because $_ will only have the ", terminating the input on the carriage return. There is more cheating to be had: adding local $/; before the input line. Now I have:

(echo '"'; echo "abcd") | perl -le 'local $/;$_=<>; $*=1; print "[$_]" +; if (/"\n^abc/) { print "true" }'
And, lo and behold, it works. But notice: I added the $/ and $* (which you shouldn't do) variables, and the \n inside your regex.

I do have to wonder, though, why you're asking this question. It has a slight odor of XY Problem ... or maybe homework. But only slight.

Replies are listed 'Best First'.
Re^2: What it mathches`
by ikegami (Patriarch) on Aug 17, 2009 at 14:18 UTC

    thus ^ is the beginning of the string because of the assumption there is only one line.

    No, ^ is the beginning of the string because /m wasn't used. No assumption was made.

    By the way, $* doesn't exist anymore.

      Reading perlre, I see this:

      1. \ Quote the next metacharacter
      2. ^ Match the beginning of the line
      (Emphasis mine.) It then goes on to say:
      By default, the "^" character is guaranteed to match only the beginning of the string, the "$" character only the end (or before the newline at the end), and Perl does certain optimizations with the assumption that the string contains only one line.
      (Emphasis still mine.) That's where I got the "optimisation" part. As to $*, I'm using perl 5.8. The OP didn't mention version of perl, and I've not been paying attention to his posting history to note what version he's been using, so felt free to use whichever perl I had handy.

        I'm missing your point. Nothing here contradicts what I said, and I don't see how anything here relates to what I said.

        so felt free to use whichever perl I had handy.

        That's fine (although it gives an error in 5.8.8). That's why I said "by the way". The goal was to discourage anyone from thinking it was acceptable to use it. It's buggy when used in conjunction with qr// and perhaps other features.