The handling of $_ is somewhat magical, and provided by the perl parser - not by the angle brackets. When perl sees this:
while (<>) {
}
It actually does this:
while (defined($_ = <ARGV>)) {
();
}
You can verify this yourself using
B::Deparse. Here is an example (assume test.pl as filename):
perl -MO=Deparse test.pl
Problems:
- By adding the test for $i <= 4 to the while loop you caused perl to skip the magical behaviour for while(<>).
- You should chomp the data that is being read in, otherwise instead of "1" you will have "1\n"
Suggestions:
- It is a better practice to use push instead of (@arr1,$_)
- use warnings instead of perl -w
New version of the code that addresses the above
#!/usr/bin/perl
use strict;
use warnings;
print "Enter 5 numbers: \n";
my $i=0;
my @arr1=();
while (<>) {
chomp;
push @arr1, $_;
last if ++$i > 4;
}
foreach my $m (@arr1) {
print $m, "\n";
}
Update
Additionally, please read
How To Ask A Question - Choose a Good, Descriptive Title