develooper Front page | perl.perl5.porters | Postings from September 2016

[perl #129277] [PATCH] addition to perlrecharclass about '$' as"special"

Thread Next
From:
James E Keenan via RT
Date:
September 15, 2016 20:08
Subject:
[perl #129277] [PATCH] addition to perlrecharclass about '$' as"special"
Message ID:
rt-4.0.24-18253-1473970081-246.129277-15-0@perl.org
On Thu Sep 15 11:34:02 2016, cpan@goess.org wrote:
> This is a bug report for perl from cpan@goess.org,
> generated with the help of perlbug 1.40 running under perl 5.25.5.
> 
> 
> -----------------------------------------------------------------
> [Please describe your issue here]
> 
> I'm suggesting a documentation change. perlrecharclass says "Most
> characters
> that are meta characters in regular expressions...lose their special
> meaning
> and can be used inside a character class without the need to escape
> them" and
> goes on to list the ones that do need to be escaped. It does *not*
> list a '$'.
> But this will not match a dollar sign or a comma:
> 
> [$,]
> 
> and it would be good advice to point out that a '$' is as special
> inside a
> character class as it is anywhere else in a regular expression.
> 
> 

While I agree with the general thrust of the patch, I think we're going to have to brainstorm for edge cases before we nail down its final wording.

Consider the following:

#####
$ cat 129277-charclass-dollar.pl 
#####
# perl
use strict;
use warnings;
use 5.10.1;

{
    my $str;
    local $/ = "\n";

    $str = 'This is a string with hard-quoted $/ in its middle.';
    say $str;
    say ( ($str =~ m{[$/]})     ? "Yes" : "No" );
    say ( ($str =~ m{[\$/]})    ? "Yes" : "No" );
    say ( ($str =~ m{[^$/]})    ? "Yes" : "No" );
    say ( ($str =~ m{[^\$/]})   ? "Yes" : "No" );

    $str = "This is a string with interpolated $/ in its middle.";
    say $str;
    say ( ($str =~ m{[$/]})     ? "Yes" : "No" );
    say ( ($str =~ m{[\$/]})    ? "Yes" : "No" );
    say ( ($str =~ m{[^$/]})    ? "Yes" : "No" );
    say ( ($str =~ m{[^\$/]})   ? "Yes" : "No" );
}

__END__
#####

Output:

#####
$ perl 129277-charclass-dollar.pl 
#####
This is a string with hard-quoted $/ in its middle.
No
Yes
Yes
Yes
This is a string with interpolated 
 in its middle.
Yes
No
Yes
Yes
#####

Is that the output we all expect?  How do we describe the behavior of the "dollar-variables" inside negated character classes?

Thank you very much.

-- 
James E Keenan (jkeenan@cpan.org)

---
via perlbug:  queue: perl5 status: new
https://rt.perl.org/Ticket/Display.html?id=129277

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About