James Taylor wrote:
> 
> I'm modifying a script someone wrote that basically reads a file file
> into STDIN, and I was curious if there was anyway of modifying the
> stdin value without having to first open the file, modify it, close the
> file, and then open it into stdin.
> 
> Basically, looks like:
> 
> open(STDIN,$filename) || die "whatever\n";
> close(STDOUT);
> 
> Well, I need to to a search/replace on STDIN, something like:
> 
> open(STDIN,$filename) || die "blah\n";
> STDIN =~ s/one/two/g;
> close(STDOUT);
> 
> Line 2 isn't intended to work, it's just there to show you what i'm
> trying to accomplish.

STDIN, STDOUT and STDERR are just file handles like any other file
handles.  The only difference is that the system opens them and connects
them to a tty when the process starts.  They don't have any "special"
powers to edit a file in-place like you seem to think they do.  The
usual Perl idiom to do in-place editing is to use the -i command line
switch or the $^I variable in a program.

perl -i -pe's/one/two/g' filename

Or:

#!/usr/bin/perl

{   local ( $^I, @ARGV ) = ( '', 'filename' );
    s/one/two/g while <>;
    }

__END__


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to