in reply to Java accepts Perl leadership

Don't be so smug. PHP has automatic iterators, bash shell scripts have it, C++ has it (though implemented in a completely different way, using lots of operator overloading), and there are probably lots more languages that have a similar construct.

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.

Replies are listed 'Best First'.
Re^2: Java accepts Perl leadership
by broquaint (Abbot) on Jun 28, 2004 at 04:33 UTC
    Provide the desired functionality then :)
    { 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
    HTH

    _________
    broquaint