Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

In-Place Editing Problem

by wlh4 (Novice)
on Oct 23, 2014 at 21:54 UTC ( [id://1104818]=perlquestion: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
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.

    - tye        

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1104818]
Approved by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-24 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found