This while loop has no closing brace:
while ($_ = <FILEHANDLE1>)
{
@array = split (/:/, <FILEHANDLE1>);
if ($array[2] eq "0")
{
print "$array[2] Superuser Account\n";
}
Secondly, you really want to write this as:
while (<FILEHANDLE1>)
{
chomp;
my @array = split /:/;
if ($array[2] eq "0")
{
print "$array[2] Superuser Account\n";
}
}
The chomp isn't strictly necessary, but it'll avoid you getting strange results with a newline hanging out in the "shell" field of the password file you're parsing, if you eventually need to process the shell.
One last stylistic suggestion, as you're still learning perl: You should try to avoid using the old two argument open, and instead use the newer three argument open. That is, instead of this:
open (FILEHANDLE2, ">text.new") || die "Can't create file named text.new: $!\n";
You should instead use the three argument form:
open (FILEHANDLE2, ">", "text.new") || die "Can't create file named text.new: $!\n";
For this particular example it doesn't make much difference. The three-argument form is safer, though, when the filename arrives in a variable.
You might also consider getting into the habit of using lexical filehandles.:
open my $filehandle1, "<", "text.old" or die "Can't open file named text.old: $!\n";
open my $filehandle2, ">", "text.new" or die "Can't create file named text.new: $!\n";
while (<$filehandle1>)
{
chomp;
my @array = split /:/;
if ($array[2] eq "0")
{
print "$array[2] Superuser Account\n";
}
}
close $filehandle2 or die "Can't close file named text.new: $!\n";
close $filehandle1 or die "Can't close the file named text.old: $!\n";
The advantage of lexical filehandles is that they stay local to whatever scope they're declared. This makes it easier to deal with files locally in a subroutine, for example.
I noticed that you weren't doing anything with the second file yet. (FILEHANDLE2 in your original code.) I assume that code's coming once you get the basics working. :-)
text.new.