...
foreach(@arr) {
@IDINFO = split(/:/,"$arr[$_]");
...
You should see lots of warnings by Perl, because $arr[$_] will always be $arr[0], because $_ is not what you might think it is.
As a first approach to finding such errors yourself, I would suggest printing $_ and $arr[ $_ ] to make sure that what you see is what you think there should be.
As a first fix, use $_ instead of $arr[ $_ ].
In general, there are many things that you can improve in your program to make it more resilient and not rely on features that were superseded by features introduced in Perl 5:
$HAN = "handle";
$FILE = "passwd.txt";
open("$HAN", "$FILE")||die "Error opening file: $!\n";
Please do not use global filehandles. Whatever textbook promotes such use, it is hopefully very old, as this is a technique used in Perl 4. Using this technique relies on the idea that nobody nowhere in your program will ever use a filehandle with a similar name. The Perl5 way is to simply use lexical filehandles:
open my $HAN, $FILE
or die "Error opening file '$FILE': $!";
Now, your file iteration logic is also problematic because it mixes two approaches:
while(<$HAN>) {
@arr = <$HAN>;
}
The while(<$HAN>) { reads one line from $HAN and stores it in $_.
The @arr= <$HAN> reads all remaining lines from $HAN and stores them in @arr.
You should use either one of the two idioms but not mix them.
...
print "UID is: @IDINFO[2] \n";
print "GID is: @IDINFO[3] \n";
...
The common way to write these is to use $ instead of @:
print "UID is: $IDINFO[2] \n";
print "GID is: $IDINFO[3] \n";
|