[comp.lang.perl] shouldn't backslash really quote?

eichin@apollo.com (Mark Eichin) (05/23/91)

The script (at the end of this message) prints:

Line == <<x_$foo>>
found x-pat
found x-brace-dollar-pat

I would have expected it to print "found x-slash-dollar-pat" as well,
or at least threeslash... I get this output with VAX/BSD perl 3.041
and 4.000, as well as Apollo Domain/OS perl 4.003. (I really do want
pattern matching, not equality checking, and (as the last if clause
shows) I should really be able to do this without eval.) Can anyone
explain to me why I can't simply backslash a dollar sign, as is
implied by the info file, 
		"Simply quote all the non-alphanumeric characters:
			     $pattern =~ s/(\W)/\\$1/g;"

#!perl
$_='x_$foo';
$pat = "foo";
print "Line == <<$_>>\n";
if(/x_.$pat/) {
	print "found x-pat\n";
} 
if(/x_\$$pat/) {
	print "found x-slash-dollar-pat\n";
} 
if(/x_\\$$pat/) {
	print "found x-twoslash-dollar-pat\n";
} 
if(/x_\\\$$pat/) {
	print "found x-threeslash-dollar-pat\n";
} 
if(/x_[\$]$pat/) {
	print "found x-brace-dollar-pat\n";
} 
					_Mark_ <eichin@apollo.hp.com>
					CCD-East

tchrist@convex.COM (Tom Christiansen) (05/23/91)

From the keyboard of eichin@apollo.com:
:The script (at the end of this message) prints:
:
:Line == <<x_$foo>>
:found x-pat
:found x-brace-dollar-pat
:
:I would have expected it to print "found x-slash-dollar-pat" as well,
:or at least threeslash... I get this output with VAX/BSD perl 3.041
:and 4.000, as well as Apollo Domain/OS perl 4.003. (I really do want
:pattern matching, not equality checking, and (as the last if clause
:shows) I should really be able to do this without eval.) Can anyone
:explain to me why I can't simply backslash a dollar sign, as is
:implied by the info file, 
:		"Simply quote all the non-alphanumeric characters:
:			     $pattern =~ s/(\W)/\\$1/g;"
:
:#!perl
:$_='x_$foo';
:$pat = "foo";
:print "Line == <<$_>>\n";
:if(/x_.$pat/) {
:	print "found x-pat\n";
:} 

This was found, which makes sense.

:if(/x_\$$pat/) {
:	print "found x-slash-dollar-pat\n";
:} 

Let's see.  This becomes /x_\$foo/.  In fact, if you use this pattern
literally, then the match works fine.  So I wonder whether it's a bug.
Larry? Are you out there?  Is the $foo screwing up and making it
forget that \$ was quoted?

:if(/x_\\$$pat/) {
:	print "found x-twoslash-dollar-pat\n";
:} 

This time you didn't escape the $, so it's anchoring
at the end of the line.  Shouldn't match.


:if(/x_\\\$$pat/) {
:	print "found x-threeslash-dollar-pat\n";
:} 

There's a literal backslash here due to \\, so it shouldn't match.  
On the other hand, it maybe might have worked around the possible 
bug in x-slash-dollar-pat, but didn't.  I guess that's good.


:if(/x_[\$]$pat/) {
:	print "found x-brace-dollar-pat\n";
:} 

Yes, and then there's that.  It should match and did.  I get the same
behavior you describe, back down to 3.018 perl.  So I guess I have the
same questions you did.

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
		"So much mail, so little time." 

allbery@NCoast.ORG (Brandon S. Allbery KF8NH) (05/24/91)

As quoted from <1991May22.202128.27037@convex.com> by tchrist@convex.COM (Tom Christiansen):
+---------------
| From the keyboard of eichin@apollo.com:
| :if(/x_\\$$pat/) {
| :	print "found x-twoslash-dollar-pat\n";
| :} 
| 
| This time you didn't escape the $, so it's anchoring
| at the end of the line.  Shouldn't match.
+---------------

Uh, wouldn't the $$ expand to the process ID of Perl, since the backslash is
escaped?

++Brandon
-- 
Me: Brandon S. Allbery			 KF8NH: DC to LIGHT!  [44.70.4.88]
Internet: allbery@NCoast.ORG		 Delphi: ALLBERY
uunet!usenet.ins.cwru.edu!ncoast!allbery

tchrist@convex.COM (Tom Christiansen) (05/24/91)

From the keyboard of allbery@ncoast.ORG (Brandon S. Allbery KF8NH):
:As quoted from <1991May22.202128.27037@convex.com> by tchrist@convex.COM (Tom Christiansen):
:+---------------
:| From the keyboard of eichin@apollo.com:
:| :if(/x_\\$$pat/) {
:| :	print "found x-twoslash-dollar-pat\n";
:| :} 
:| 
:| This time you didn't escape the $, so it's anchoring
:| at the end of the line.  Shouldn't match.
:+---------------
:
:Uh, wouldn't the $$ expand to the process ID of Perl, since the backslash is
:escaped?

Yup, perl seems to scan left to right here, so would pick up $$ before $pat.

Oh well.

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
		"So much mail, so little time."