...I take in a text file (words.txt) where there is a new word each line.
If each line is one word, splitting isn't necessary. Adapting your script, you can do the following:
#!/usr/bin/env perl
use strict;
use warnings;
open my $words, '<', 'words.txt' or die $!;
while ( my $word = <$words> ) {
print $word;
}
print "\n\n";
close $words;
You may have noticed from the excellent responses that lexical variables (my) are used. It's best to follow this practice--even with file handles. Also, always use strict; and use warnings;. It's also best to handle open errors, in case there was a problem opening a file.
Hope this helps! |