Josh Rosenbaum wrote:
> While we're looking at a new release, perhaps now is a proper time to get
> some vmethods in that seem to be missing for everyone. I have added both
> substr for scalars and delete for hash ops, as I'm sure many others have
> done. (Actually I just looked at CVS and it looks like you already added
> delete.)
Hi Josh,
Yes, I've added hash.delete(key) and scalar.remove(pattern) which does
the same thing as scalar.replace(pattern, '')
I also added substr to the String plugin, but forget to add it as a string
vmethod (one reason why this is all unified in TT3 - to avoid all this
duplication). Thanks for the reminder.
> -----------------------------------------------
> sub {
> my ($scalar, $offset, $length) = @_;
> return substr($scalar, $offset, $length);
> };
> -----------------------------------------------
Unfortunately it's not as simple as that. Perl distinguishes between
substr($scalar, $offset, undef) and substr($scalar, $offset) so we have
to hard-code the alternatives (unless anyone knows another way?). I've
also added code the handle the 4th argument to provide a replacement string,
thanks to http://rt.cpan.org/Ticket/Display.html?id=2619
'substr' => sub {
my ($text, $offset, $length, $replacement) = @_;
$offset ||= 0;
if(defined $length) {
if (defined $replacement) {
my $removed = substr( $text, $offset, $length );
substr( $text, $offset, $length ) = $replacement;
return $removed;
}
else {
return substr( $text, $offset, $length );
}
}
else {
return substr( $text, $offset );
}
},
I think that covers all bases. Damn shame when something that should be
so simple turns out so messy. Ho hum, that's Perl sometimes.
> In addition, I just had another thought of something that seems to be
> missing and a cause of problems on the mailing list occasionally. That
> would be missing back references in replace regular expressions.
Yeah, this is a nuisance, but I haven't yet seen an implementation that
solves all the problems securely and robustly. I'm not keen on adding a
new syntax for back-references. What was your reasoning for that over
the regular $1 or \1 syntax?
I'll give it some more thought. I'll also dig through the archives and
see what other proposals we've had to solve this. Any of those people
who have made suggestions are welcome to chip in again here, to refresh
our memories if nothing else.
Cheers
A
_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates