in reply to Finally, a $& compromise!

Nice, but after some playing around I can guarantee that you have solved the least of my problems.

What I really want is conditional support for $` and $'. That is much worse.

To give a simple example, try the following 3 programs:

#!/usr/bin/perl # This demonstrates matching through a string use strict; use Time::HiRes qw(gettimeofday); my $start = gettimeofday(); my $str = "_" x $ARGV[0]; 1 while $str =~ /./g; my $elapsed = gettimeofday() - $start; print "$ARGV[0] characters took $elapsed seconds\n"; #!/usr/bin/perl # This demonstrates matching through a string, capturing use strict; use Time::HiRes qw(gettimeofday); my $start = gettimeofday(); my $str = "_" x $ARGV[0]; 1 while $str =~ /(.)/g; my $elapsed = gettimeofday() - $start; print "$ARGV[0] characters took $elapsed seconds\n"; #!/usr/bin/perl # This demonstrates matching through a string, capturing $` use strict; use Time::HiRes qw(gettimeofday); # Mess life up here if ("gotcha" =~ /o/) { my $fooey = $`; } my $start = gettimeofday(); my $str = "_" x $ARGV[0]; 1 while $str =~ /(.)/g; my $elapsed = gettimeofday() - $start; print "$ARGV[0] characters took $elapsed seconds\n";
If you try it you will find that the first two versions run linearly, with only a modest speed difference for the capturing. But the third is a quadratic speed drop. Should you ever, as I do, use REs as a way of tokenizing, this means that the use of a single $` or $', anywhere, turns linear algorithms quadratic. By contrast turning $& on and off is not going to change your program's scalability.

For that reason if I write something for other people's use, I would really like the option of turning off $` and $' lexically. Because I want access to $1 without being hammered with $` and $'.

Replies are listed 'Best First'.
(tye)Re2: Finally, a $& compromise!
by tye (Sage) on Nov 28, 2001 at 20:52 UTC

    As already mentioned, $` and $' are controlled by the same flag and so the pragma already affects them as well.

            - tye (but my friends call me "Tye")
      As specifically pointed out in the section Capturing still works, the use of $1 and friends turns support for them back on, despite the pragma.

      I did read what japhy wrote before I posted. While it is nice to turn off the production of capturing strings, the main time I do enough matching that I really care about capturing or not is tokenizing, when I am almost definitely going to be using capturing matches. So this module helps with the problem, but not in the only case where I care.

        "sawampersand" is a single bool. I can't come up with any reason that this code would need to have "sawampersand" be treated as "true" when capturing is being done. I think either japhy was not being completely clear when he wrote that paragraph or he misunderstood some subtleties about how $& and capturing interact.

        Sorry you felt that I was accusing you of not reading.

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