Here's what I got to work. It's pretty self-explanatory.
#!/tmp/bleadperl/bin/perl -w
use re 'eval';
my $re;
my $len = 20;
$_ = "this is a long string, and I want to insert newlines every 20 ch
+ars";
$re = qr{
(??{ '\b\w{1,' . ($len - ($+[0] - $-[0])) . '}\b' })
(?(?{ ($+[0] - $-[0]) < $len })
(??{ '\W{0,' . ($len - ($+[0] - $-[0])) . '}' })
(?(?{ ($+[0] - $-[0]) < $len })
(?(?=
(??{ '\b\w{1,' . ($len - ($+[0] - $-[0])) . '}\b' }))
(??{ $re })
)
)
)
}x;
s{($re)}{$1\n}xg;
print;
What I want to be able to say is something like:
$re = qr{
(??{ '\b\w{1,' . ($len - ($+[0] - $-[0])) . '}\b' })
(?(?{ ($+[0] - $-[0]) < $len })
(??{ '\W{0,' . ($len - ($+[0] - $-[0])) . '}' })
)
(?(?{ ($+[0] - $-[0]) == $len })
(?&done)
)
}x;
s{($re+)(?%done)}{$1\n}xg;
That code creates two new regex assertions, (?%LABEL) and (?&LABEL). The first defines a position in the regex, and the second forcibly moves the regex engine to that position. Now, perhaps this is too much power to wield, but I like power. If people don't like this, then I would do something like:
$re = qr{
(??{ '\b\w{1,' . ($len - ($+[0] - $-[0])) . '}\b' })
(?(?{ ($+[0] - $-[0]) < $len })
(??{ '\W{0,' . ($len - ($+[0] - $-[0])) . '}' })
)
(?(?{ ($+[0] - $-[0]) == $len }) (?;) )
}x;
s{($re+)}{$1\n}xg;
Here, the (?;) assertion means "jump out of the enclosing quantifier". This means that we're making a while loop out of the quantifier:
while (1)
do_pattern($re);
if ($seen_qu_semi) { last }
}
Again, perhaps this is too much power. But I really like this power. However, perhaps I could simply develop some rules by which to munge the label/goto into an ugly format; or at least the out-of-quant into an ugly format.
In the words of Tim "The Tool Man" Taylor, "more power! arr arr arr!"
Update: I found that I could write it like so:
$re = qr{
(??{ '\b\w{1,' . ($len - ($+[0] - $-[0])) . '}\b' })
(?(?{ ($+[0] - $-[0]) < $len })
(??{ '\W{0,' . ($len - ($+[0] - $-[0])) . '}' })
)
}x;
s{
(
(?:
(?(?{ ($+[0] - $-[0]) == $len })(?!))
$re
)+
)
}{$1\n}xg;
That seems easier to read. Plainer. More logical. It's a real while loop. I like it. Perhaps I'll create a filter.
_____________________________________________________
Jeff[japhy]Pinyan:
Perl,
regex,
and perl
hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??; |