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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.