On Sat, Jan 27, 2001 at 05:13:23PM -0500, Michael G Schwern wrote:
> On Sat, Jan 27, 2001 at 03:42:43PM -0700, root wrote:
> > I read RFC195 suggesting to drop 'chop' and go with 'chomp'.
> > What does 'chop' have anything to do with 'chomp'?
>
> chop() and chomp() are very often confused due to their similar names,
> similar functionality and the fact that chop() did chomp()'s job
> (poorly) prior to perl5. Nowdays, chop() is very often used where
> they really ment chomp().
>
> Actually, the docs are somewhat to blame for this. perlfunc is loaded
> with chop misuses. Will fix.
>
> The complex nature of the complete chop() is unfortunate, it makes it
> very difficult to reimplement and prototype. The basic version,
> however, isn't hard:
>
> sub chop (;\$) {
> my($var) = @_ ? $_[0] : $_;
> return substr($var, -1, 1, '');
> }
That doesn't modify its arguments (nor does it modify $_).
> Once you start getting into chopping lists and hashes, it becomes
> impossible to prototype.
This one not only modifies its arguments (or $_ when called without),
it also has the right prototype and works on lists:
sub chop (@) {
my $__;
map {$__ = substr $_ => -1, 1, ""} @_ ? @_ : $_;
$__;
}
Aliasing is a cute thing.
Abigail