meant is there some Perl's simple internal variable or feature, not resort to some added line code/variable must be done by coder ?
Having wished for this myself, I can say that the answer to the general question is unfortunately: No, with the exception of eof when reading from files, as mentioned by kcott.
TIMTOWTDI, though. You haven't shown any example code so it's really hard to say which variation below (if any) might be most appropriate or easiest in your case.
use warnings;
use strict;
my @array = qw/ Foo Bar Quz /;
# C-style loop, not very perlish (but *sometimes* useful)
for ( my $i = 0; $i < @array; $i++ ) {
print $array[$i], $i==$#array ? ".\n" : ", ";
}
# the perlish variant of the above
for my $i (0..$#array) {
print $array[$i], $i==$#array ? ".\n" : ", ";
}
use 5.012; # so keys/values/each work on arrays
while ( my ($i, $v) = each @array ) {
print $v, $i==$#array ? ".\n" : ", ";
}
# as suggested by jwkrahn
my $cnt = @array;
for my $v (@array) {
print $v, --$cnt ? ", " : ".\n";
}
# or sometimes it's as simple as "join"
print join(", ", @array), ".\n";
my @array_copy = @array; # because it's destructive!
while ( my $v = shift @array_copy ) {
print $v, @array_copy ? ", " : ".\n";
}
And lastly, while it might seem overkill, the concept of iterators can be extremely useful in some cases. The book Higher-Order Perl is a great read on this and other topics. Here, I've extended the concept with a "peekable" iterator class. One can even overload the <> operator as I showed here (note one does need Perl v5.18 or better for the overloaded <> to work in list context). Update: In the following code, I've replaced my own classes with Iterator::Simple and Iterator::Simple::Lookahead. /Update
# demo of a basic iterator
use Iterator::Simple 'iter';
my $it = iter(\@array);
while (defined( my $v = $it->() )) {
print $v, " ";
}
print "\n";
# demo the peekable iterator
use Iterator::Simple::Lookahead;
my $itp = Iterator::Simple::Lookahead->new( iter(\@array) );
while (defined( my $v = $itp->() )) {
print $v, defined $itp->peek ? ", " : ".\n";
}
Bonus: For some experimental stuff, see FIRST and LAST in Perl6::Controls.
Also minor edits to wording.