Symbols in patterns interpolated *recursively*? 
Author Message
 Symbols in patterns interpolated *recursively*?

Hi,

Sorry.  I originally posted on comp.lang.perl.misc.  Then someone
encouraged me to post here because it appeared to be a serious
question about how the Win32 port of Perl handles patterns invoving
variables with backslashes.

I was trying to compare Windows paths using patterns in an Active
State Perl 5.6.1 script running on Win2000ProSP3.  It appears to me
that the backslashes in the content of a variable initialed with a
path doesn't work in a pattern.

I created the followed test script in which all four patterns should
match, IMHO.  If the initial content has no backslashes or a pair, the
patterns match.  But the two cases that have a single backslash fail.
Is this a Perl bug or am I in error?

TIA,
Richard

========= Script =============

my $A_B_C = "abc";
$_ = $A_B_C;
print   "\$A_B_C     " .
        (/^$A_B_C$/? "matches" : "does not match") .
        " \"$A_B_C\"\n";            # Matches

my $ABC_DEF = "abc\\def";
$_ = $ABC_DEF;
print   "\$ABC_DEF   " .
        (/^$ABC_DEF$/? "matches" : "does not match") .
        " \"$ABC_DEF\"\n";  # Doesn't match; why?

print   "\${ABC_DEF} " .
        (/^${ABC_DEF}$/? "matches" : "does not match") .
        " \"${ABC_DEF}\"\n"; # Doesn't match; why?

my $ABC_XXX = "abc\\\\def";
print   "\$ABC_XXX   " .
        (/^$ABC_XXX$/? "matches" : "does not match") .
        " \"$ABC_XXX\"\n"; # Matches

=========== Result ==============

$A_B_C     matches "abc"
$ABC_DEF   does not match "abc\def"
${ABC_DEF} does not match "abc\def"
$ABC_XXX   matches "abc\\def"



Sat, 26 Mar 2005 09:02:28 GMT  
 Symbols in patterns interpolated *recursively*?

Quote:

> Subject: Symbols in patterns interpolated *recursively*?

That is not what is happening.

There is only one round of interpolation on the m//, the string
resulting from the interpolation is further processed by
the regex engine.

Quote:
> Sorry.  I originally posted on comp.lang.perl.misc.  Then someone
> encouraged me to post here because it appeared to be a serious
> question about how the Win32 port of Perl handles patterns invoving
> variables with backslashes.

There is nothing win32-specific in your problem as far as I can tell.

Quote:
> I was trying to compare Windows paths using patterns in an Active
> State Perl 5.6.1 script running on Win2000ProSP3.  It appears to me
> that the backslashes in the content of a variable initialed with a
> path doesn't work in a pattern.

If you use forward slashes in the paths, then you don't need
to be concerned with backwards slashes at all.  :-)

Forward slashes work fine as the directory separator on Windows.
It is only the _command interpreter_ that demands backwards ones
(because forward ones are used for something else).

Quote:
> I created the followed test script in which all four patterns should
> match, IMHO.  If the initial content has no backslashes or a pair, the
> patterns match.  But the two cases that have a single backslash fail.
> Is this a Perl bug or am I in error?

You are in error.

Quote:
> my $ABC_DEF = "abc\\def";

If you really need to work with backslashes, then using single quotes
instead of doubles will make your life easier.

Quote:
> $_ = $ABC_DEF;
> print      "\$ABC_DEF   " .
>    (/^$ABC_DEF$/? "matches" : "does not match") .
>    " \"$ABC_DEF\"\n";  # Doesn't match; why?

The pattern is:  abc\def

The string to match against is:  abc\def

Pattern requires an "a" followed by a "b" followed by a "c"
followed by a digit character...

The string has no digit characters in it, so the match must fail.

You can fix it by calling quotemeta() on $ABC_DEF before doing
the match, or by:

        /^\Q$ABC_DEF\E$/

But disguising a test for equality as a pattern match is
obfuscation, so don't do that in a serious program:

   ( $_ eq $ABC_DEF ? "matches" : "does not match") .

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas
.



Mon, 28 Mar 2005 20:57:09 GMT  
 Symbols in patterns interpolated *recursively*?
Tad McClellan writes ..

Quote:

...
>> I was trying to compare Windows paths using patterns in an Active
>> State Perl 5.6.1 script running on Win2000ProSP3.  It appears to me
>> that the backslashes in the content of a variable initialed with a
>> path doesn't work in a pattern.

>If you use forward slashes in the paths, then you don't need
>to be concerned with backwards slashes at all.  :-)

You still regularly need to deal with back slashes when capturing the
output of system commands on Win32 systems.

Quote:
>Forward slashes work fine as the directory separator on Windows.
>It is only the _command interpreter_ that demands backwards ones
>(because forward ones are used for something else).

"demands" might be misleading - you can enclose forward slashes in  
double-quotes to let them skip past the CMD.EXE parser.

--
  Jason King
  To demunge email: strip invalid, ROT13, add com



Thu, 31 Mar 2005 16:57:08 GMT  
 Symbols in patterns interpolated *recursively*?

Quote:

> Tad McClellan writes ..

> ...
>>> I was trying to compare Windows paths using patterns
>>If you use forward slashes in the paths, then you don't need
>>to be concerned with backwards slashes at all.  :-)

> You still regularly need to deal with back slashes when capturing the
> output of system commands on Win32 systems.

Right (because the data comes from the command interpreter).

So you do

   tr#\\#/#;

on the paths, then you don't need to be concerned with them once again.

:-)

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas
.



Thu, 31 Mar 2005 21:42:30 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Choosing a replacement to MS Access

2. Printing problem with Quick Report

3. Symbols in patterns interpolated *recursively*?

4. \Q in an interpolated pattern

5. Solaris DB_File.so: symbol dbopen: referenced symbol not found

6. Migrate from Clipper 5.2e

7. Zachary's Crystal Reports "Deal"

8. Database Formats

9. Capturing Standard Input from a Web server?

10. Help! Data-Aware BLOB fields

11. Wierd behavior for ACCESS, DELPHI 3 & ODBC

12. (no subject)

 

 
Powered by phpBB® Forum Software