On Mon, 15 Mar 2004, 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.
>
> Thanks for your help!
>
>From reading some of the other replies, I gather what you'd like to do is
modify the STDIN going into another, unmodifiable, script/program.
Correct? One possibility is:
myscript input.file | oldscript
Where "myscript" is:
#!/usr/bin/perl
while(<>) {
s/one/two/g;
print;
}
This reads "input.file" and changes all occurrances of "one" to "two" and
prints them to STDOUT. The pipe then sends this data on to "oldscript".
Another possibility:
#!/usr/bin/perl
open(fh,"|oldscript") or die "Cannot open pipe to oldscript: $!";
while (<>) {
s/one/two/g;
print fh;
}
You'd then invoke this as:
myscript input.file
Is this what you wanted?
--
Maranatha!
John McKown
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>