People, mostly Java people, have sometimes complained about Perl's foreach my $var ( @array ){} coding style.
Reading an article on the new features of Java 1.5, I'm please to note that Java has followed the lead of Perl, and added a foreach construct:
for ( type var : collection_var ) {
var.method( .... );
}
The ':' provides the iterator behind th background, so you don't have to confuse your code with an implementation detail.
--
TTTATCGGTCGTTATATAGATGTTTGCA
Fixed <em> tag per author's request - [dvergin] 2004-07-02
Actually, this exact functionality is not suported by Perl, except for internal containers (ie, straight hashes and arrays, or tie'd versions thereof). I mean, this code:
my $container = Container->new(qw(e1 e2 e3));
foreach ($container) {
# now $_ is $container, NOT one of the elements in it
}
Is not equivalent to:
my @container = qw(e1 e2 e3);
foreach (@container) {
# now $_ iterates over the elements of @container
}
While in java 1.5 you CAN switch the container between a "full-blown" container object and a "basic" array.
The only remarkable thing about the implementation of this feature in Java, is that it wasn't there in Java 1.0 (probably because the standard container interfaces weren't introduced until 1.2 or thereabouts - big oversight by Sun, if you ask me).
Anyway, I don't think the introduction of a foreach-like construct in Java kan be considered an "acceptance of Perl leadership" whatever that means.
{
package Container;
use overload (
'@{}' => sub { return [sort keys %{$_[0]}] },
fallback => 1,
);
sub new {
return bless { map { $_ => 1 } @_[1 .. $#_] }, $_[0];
}
}
my $c = Container->new( qw/ e1 e2 e3 / );
print "member $_\n"
for @$c;
__output__
member e1
member e2
member e3
People, mostly Java people, have sometimes complained about Perl's foreach my $var ( @array ){} coding style.
Eh? Where are these complaints of which you speak? Of the many complaints against Perl that I hear from Java people (e.g., line noise punctuation, poor object model, lack of typing), foreach hasn't ever been mentioned.
perlmonks.org content © perlmonks.org and broquaint, dws, Joost, TomDLux
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03