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

Replies are listed 'Best First'.
Re: Java accepts Perl leadership
by Joost (Canon) on Jun 27, 2004 at 16:01 UTC
    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.

      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

Re: Java accepts Perl leadership
by dws (Chancellor) on Jun 27, 2004 at 16:44 UTC

    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.