jfjobidon has asked for the wisdom of the Perl Monks concerning the following question:

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; }

Replies are listed 'Best First'.
Re: fileHandle vs prompt
by Anonymous Monk on Sep 17, 2013 at 18:16 UTC

    Be explicit, and say <STDIN> to read from there instead of a file.

      It works fine now! Thanks a lot

      JF

Re: fileHandle vs prompt
by Marshall (Canon) on Sep 18, 2013 at 01:33 UTC
    I may have not understood your script, but this is the general idea:

    #!/usr/bin/perl -w use strict; # usage: AskAtom.pl SomeFile # First line of "SomeFile" is the output file name. # Program asks the user for a value which is # appended to each line of the subsequent lines # in "SomeFile". my ($fileIN) = @ARGV; #first command line arg is $fileIN open(my $in, "<", $fileIN) or die "Can't open $fileIN: $!"; #The $newFile name is on the first line of "somefile" my $newFile = <$in>; #read the first line of $fileIN $newFile =~ s/^\s*//; #delete leading spaces (optional) $newFile =~ s/\s*$//; #delete trailing spaces (also does chomp()) my $atom = ask_user4atom(); open (my $out, ">", $newFile) or die "Can't write to $newFile :$!"; while (<$in>) #reading of <$in> starts again at 2nd line { chomp; #deletes line endings next if /^\s*$/; #skip blank lines (a blank line at the end # of the file is not an error) print $out "$_-$atom\n"; #append $atom to each line } #When asking user a question - blank line is just a #re-prompt. Spaces before or after the user input #also do not matter...That is standard CMD Line... sub ask_user4atom { my $atom; while ( (print "Enter atom symbol: "), $atom =<STDIN>) { next if $atom =~ /^\s*$/; #re-prompt on a blank line $atom =~ s/^\s*//; #delete leading spaces $atom =~ s/\s*$//; #delete trailing spaces return $atom; } } #Note: In a short running script like this, there is # no need to explictly close() files. The O/S will do that # when the script finishes - no biggie. # A File Handle is an O/S resource, but here this is not # significant. =Example file: ChemIn.txt Chemout.txt ABC XYZ =End ChemIn.txt =Example Command Line Output C:\TEMP>perl askatom.pl ChemIn.txt Enter atom symbol: Enter atom symbol: Na C:\TEMP>type ChemIn.txt ChemOut.txt ABC XYZ C:\TEMP>type ChemOut.txt ABC-Na XYZ-Na =END of Example Command Line Output