wlh4 has asked for the wisdom of the Perl Monks concerning the following question:
I'm looking for help on where to start looking for the solution to a problem.
I have a large amount of code, but I created a little subroutine to receive a file name (full path) and a substitution. The object is to open the file using the $^I variable for in-place editing, substitute some code for the passed in code, and then exit the subroutine. Very, very simple. I place the file name into @ARGV by simple assignment statement and then do a while ( <> ) { s/../../; print } loop. Very, very simple.
The problem is that when it is run, Perl first asks for input from STDIN; when I hit Ctrl-D, it then opens the file and makes the substitution. I have made sure that the only thing in @ARGV is the file I want to change.
I tried an experiment by copying the subroutine code verbatim and placing it into a separate file, made it executable, and then called it from within the subroutine, and it works without asking for input from STDIN.
What could possibly be making my subroutine ask for STDIN if the code is run from within my program, but not when it is run from a separate process?
Thanks for any help.
wlh
Re: In-Place Editing Problem (*ARGV)
by tye (Sage) on Oct 24, 2014 at 01:30 UTC
|
If *ARGV is already open, then the next <> would continue reading from it before looking for the next file name in @ARGV. So you probably used <> before you called the subroutine that you described.
You could close *ARGV, but that would disrupt the other code that was already using <> to read from STDIN. So you might instead want to do local( *ARGV ) in your subroutine before you assign to @ARGV.
| [reply] [d/l] [select] |
Re: In-Place Editing Problem
by wlh4 (Novice) on Oct 24, 2014 at 02:59 UTC
|
Thank you for your help. I found the problem. I used 'readline' in another other subroutine to gather some input from STDIN, but failed to include a file handle. That routine worked fine, but apparently it did not close properly when the code exited the routine and so when I called the subroutine above, it continued to read from STDIN. The problem was fixed by changing 'readline' to <STDIN>. It appears that when I used 'readline' without a file handle, it used whatever was in $_ as a file handle and then didn't close it upon returning from the subroutine? Anyway, good lesson learned. | [reply] |
|