I think you're confusing yourself by using the term "split" in two different ways - one of which has nothing to do with splitting. You don't "split" a filehandle into an array, but you can slurp it - which is exactly what you're doing above, with the '@l=<FH3>' line. Perhaps what you mean is that you would like to process the array that you've created by slurping, one element (i.e., one line) at a time:
for my $line (@l){
chomp $line; # Remove the newline from the end
print $line . " - processed by 'trenchwar'\n"; # Add a little co
+ntent to each line
}
Or you could do the same thing by using Perl's implicit iterator, '$_':
for (@l){
chomp;
print "$_ - processed by 'trenchwar'\n";
}
or even by using the 'map' function:
chomp @l; # Remove the newline from all elements
print map { "$_ - processed by 'trenchwar'\n" } @l;
I note, by the way, that your script has several doubtful constructs in it:
open (FH3, ">lessons.txt");
### You should *always* check the return from any calls to the system.
open FH3, ">lessons.txt" or die "lessons.txt: $!\n";
### You should also close this filehandle; don't assume that your file
+ has actually
### been written before you do so!
close FH3;
$l="lessons.txt";
open(FH3, $l) || die("Could not open file!");
### Don't create variables that you're only going to use once; just us
+e the value.
### *And* check the return of the call. And don't pre-assume the reaso
+n for failure:
### find out what it is with the '$!' (actual error) variable.
open FH3, "lessons.txt" or die "lessons.txt: $!\n";
Update: Added a description of the errors in the script.
--
Human history becomes more and more a race between education and catastrophe. -- HG Wells
|