I get the file name by passing an argument to the script but when I want a prompt to ask a question to the user, the script reads the first fileHandle (used to read the file) and returns the first line of that file!!!
#!/usr/bin/env perl # this script takes one argument : the name of a text file # it reads every line and put them in a variable # then it ask for a atomic symbol and append it to the variable # PROBLEM: the function "askAtomSymbol" reads the wrong fileHandle!!!! # use this script like: # perl someTextFile.txt use strict; use warnings; # I get the file name with the only one argument my $fileIN = $ARGV[0]; # I open a file, make some treatment to each line and create a new fil +e # I make sur the fileHandle is closed after the loop is done my $currentLine; my $theFile = ""; open(my $in, "<", $fileIN) or die "Can't open $fileIN: $!"; while (<$in>) { $currentLine = $_; $theFile .= $currentLine; } close($in) or die "Can't close $fileIN: $!"; # I call a function to create a new file # in this exemple I oversimplify this function # the fileHandle <$in> should be closed but it seems not... my $finalFile = buildFile($theFile); print $finalFile; #This function ask for an atom symbol #and append it to the file passed in argument sub buildFile { my ($newFile) = @_; my $atom = askAtomSymbol(); # returns the first line of the file p +assed in script argument <$in>!!!!! #my $atom = "C"; # if I choose this simple line instead +, everything is fine print "\natom: --$atom--\n\n"; $newFile = $newFile . $atom . "\n"; return $newFile; } sub askAtomSymbol { my $atomSymbol; print "Enter atom symbol:\n >"; chomp ($atomSymbol = <>); # no prompt at the screen, it reads th +e first line of <$in> !!!!! print "atom: --$atomSymbol--\n"; return $atomSymbol; }
In reply to fileHandle vs prompt by jfjobidon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |