[Israel.pm] What is happening here (or -why) ?

Yuval Kogman nothingmuch at woobling.org
Mon Jun 7 07:58:43 PDT 2010

On 7 June 2010 14:42, Yossi Itzkovich <Yossi.Itzkovich at ecitele.com> wrote:
> Does it means there are two passes on the reg ex: one that  interpolates
> the dollar as a scalar, and the second that treats it as a regex
> metacharacter ?
Sort of.

There are two passes, but there are two '$' characters, which are distinct.

The first is the $ that appears in the program that signifies a variable,
and the second is the $ that was read from input was inside the variable.

To compile the regex Perl first interpolates variables (presumably
containing subpatterns), so:

    my $input = ".*"; # or better written as qr/.*/;

is the same as:


However, in your program contains the *string* '$input', which will not be
further interpolated (unless you manually call string eval).

Once the regex is constructed as a string, it's parsed with the regex
syntax. At this point the '$' is a zero width assertion, and the string
"input" found after it is a string literal.

This doesn't really make sense for line by line matching, but the pattern
'$input' could be useful using the /m modifier (matching a line starting
with "input" that isn't the first line in the string).

In order to match a literal $ you need to escape it in the pattern,
regardless of whether or not you use interpolation.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.perl.org.il/pipermail/perl/attachments/20100607/88c24f5a/attachment-0001.htm 

More information about the Perl mailing list