T
Tassilo v. Parseval
Hi,
I am currently working on something that should be backward-compatible
at least up to 5.00503. Unfortunately, this relies on @+ and @- which
weren't there by that time. So I have to find a way to create and
populate these two arrays with 5.00503. Consider the original code that
assumes that @(+|-) exist:
sub bla {
my ($string, $pat, $code) = @_
while ($string =~ /$pat/g) {
$code->(@- > 1 ? () : substr($string, $-[0], $+[0] - $-[0]),
map substr($string, $-[$_], $+[$_] - $-[$_]), 1 .. $#-);
};
}
This is essentially what Ruby's scan() does: Scan a string for a pattern
and call the code via the reference. If the pattern contains
subpatterns, call it like
$code->($1, $2, ...);
otherwise (that means, no captured subpatterns) do
$code->($&);
This means, I need the whole of @- and @+, and not just the first
element of each two. My question is specifically about generating
elements 1 to $#-. My current solution:
while ($string =~ /($pat)/g) {
@- = @+ = (); # clear previous match offsets
# populates $-[0] and $+[0]
push @-, index($string, $1);
push @+, pos($string);
# fill @-[1..#@-] and @+[1..#@+]
my $digit = 2; # $1 is the whole match
while () {
no strict 'refs';
if (defined $$digit) {
# extract offsets of $$digit
...
$digit++;
} else {
last;
}
}
$code->(@- > 1 ? () : substr($string, $-[0], $+[0] - $-[0]),
map substr($string, $-[$_], $+[$_] - $-[$_]), 1 .. $#-);
}
The part I'm uneasy about is
if (defined $$digit) {
...
}
Specifically, can $2 be undefined but $3 still contain a submatch? I
vaguely remember that I had such cases but I can't reproduce them now.
If they exist, I can't use the above code and need something better. If
so, what would be a correct solution?
Thanks in advance for any pointers,
Tassilo
I am currently working on something that should be backward-compatible
at least up to 5.00503. Unfortunately, this relies on @+ and @- which
weren't there by that time. So I have to find a way to create and
populate these two arrays with 5.00503. Consider the original code that
assumes that @(+|-) exist:
sub bla {
my ($string, $pat, $code) = @_
while ($string =~ /$pat/g) {
$code->(@- > 1 ? () : substr($string, $-[0], $+[0] - $-[0]),
map substr($string, $-[$_], $+[$_] - $-[$_]), 1 .. $#-);
};
}
This is essentially what Ruby's scan() does: Scan a string for a pattern
and call the code via the reference. If the pattern contains
subpatterns, call it like
$code->($1, $2, ...);
otherwise (that means, no captured subpatterns) do
$code->($&);
This means, I need the whole of @- and @+, and not just the first
element of each two. My question is specifically about generating
elements 1 to $#-. My current solution:
while ($string =~ /($pat)/g) {
@- = @+ = (); # clear previous match offsets
# populates $-[0] and $+[0]
push @-, index($string, $1);
push @+, pos($string);
# fill @-[1..#@-] and @+[1..#@+]
my $digit = 2; # $1 is the whole match
while () {
no strict 'refs';
if (defined $$digit) {
# extract offsets of $$digit
...
$digit++;
} else {
last;
}
}
$code->(@- > 1 ? () : substr($string, $-[0], $+[0] - $-[0]),
map substr($string, $-[$_], $+[$_] - $-[$_]), 1 .. $#-);
}
The part I'm uneasy about is
if (defined $$digit) {
...
}
Specifically, can $2 be undefined but $3 still contain a submatch? I
vaguely remember that I had such cases but I can't reproduce them now.
If they exist, I can't use the above code and need something better. If
so, what would be a correct solution?
Thanks in advance for any pointers,
Tassilo