in reply to Re^3: @ in regex, or not?
in thread @ in regex, or not?

More fun with Deparse and the all too fleeting concept of special characters, giving further evidence that our character set is perhaps just not rich enough for Perl's liking ;-):
=== 1 === perl -MO=Deparse -e "$_='abc@de';print if(/[_@]/)" $_ = 'abc@de'; print $_ if /[_\@]/; -e syntax OK perl -e "$_='abc@de';print if(/[_@]/)" abc@de === 2 === perl -MO=Deparse -e "$_='abc@de';print if(/[@_]/)" $_ = 'abc@de'; print $_ if /[@_]/; -e syntax OK perl -e "$_='abc@de';print if(/[@_]/)" /[]/: unmatched [] in regexp at -e line 1. === 3 === perl -MO=Deparse -e "$_='abc@de';print if(/[_@a]/)" In string, @a now must be written as \@a at -e line 1, near "[_@a" -e had compilation errors. $_ = 'abc@de'; print $_ if /[_@a]/; === 4 === perl -e "@a[0]='z';@a[23]='oob';$_='abc@de';print if(/[_@a]/)" abc@de
My guess: In the first example, Perl has escaped the @ itself since it saw no array there, but in the second example, it saw an array, and accepted the syntax, but in evaluation it vanished. In the third case, Perl knows there is no @a array and barfs, telling be to be more specific, however if @a does exist (example 4), Perl proceeds, though perhaps not doing what the author intended - it concatenates all the elements of @a and matches against the resulting string (that I didn't expect).

Usually when I play around like this I learn something interesting, but as often as not I also end up with new questions!

--
I'd like to be able to assign to an luser

Replies are listed 'Best First'.
(tye)Re: @ in regex, or not?
by tye (Sage) on Mar 28, 2001 at 05:11 UTC

    The regex parser has some rather convoluted heuristics that it follows to decide whether $ or @ is followed by a variable name. One side effect of this is that /[$]/ is an error.

    The only real documentation of this deep magic is in the source code (simply search for "weight" as it is only used on that code -- in toke.c, BTW).

            - tye (but my friends call me "Tye")