in reply to Finally, a $& compromise!
What I really want is conditional support for $` and $'. That is much worse.
To give a simple example, try the following 3 programs:
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.#!/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";
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 | |
by tilly (Archbishop) on Nov 28, 2001 at 21:06 UTC | |
by tye (Sage) on Nov 28, 2001 at 21:39 UTC | |
by tilly (Archbishop) on Nov 28, 2001 at 21:55 UTC | |
by japhy (Canon) on Nov 28, 2001 at 22:31 UTC |