Modifying STDIN

2004-03-15 Thread James Taylor
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!

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: Modifying STDIN

2004-03-15 Thread Randy W. Sims
On 03/16/04 00:06, 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.
I think what you want is a traditional filter:

=== myfilter.pl ===
#!/usr/bin/perl
use strict;
use warnings;
while (my $line = <>) {
  chomp($line);
  $line =~ s/^/sprintf("%3s ", $.)/e;
  print "$line\n";
}
__END__

piping it to its own STDIN produces

cat myfilter.pl | perl myfilter.pl

  1 #!/usr/bin/perl
  2
  3 use strict;
  4 use warnings;
  5
  6 while (my $line = <>) {
  7   chomp($line);
  8   $line =~ s/^/sprintf("%3s ", $.)/e;
  9   print "$line\n";
 10 }
See perldoc perlopentut, section "Filters"

Regards,
Randy.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: Modifying STDIN

2004-03-16 Thread James Taylor
I actually can't do it that way, this is a part of a custom perl module 
that someone wrote, and those are just 2 lines from the module.

On Mar 15, 2004, at 11:01 PM, Randy W. Sims wrote:

On 03/16/04 00:06, 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.
I think what you want is a traditional filter:

=== myfilter.pl ===
#!/usr/bin/perl
use strict;
use warnings;
while (my $line = <>) {
  chomp($line);
  $line =~ s/^/sprintf("%3s ", $.)/e;
  print "$line\n";
}
__END__

piping it to its own STDIN produces

cat myfilter.pl | perl myfilter.pl

  1 #!/usr/bin/perl
  2
  3 use strict;
  4 use warnings;
  5
  6 while (my $line = <>) {
  7   chomp($line);
  8   $line =~ s/^/sprintf("%3s ", $.)/e;
  9   print "$line\n";
 10 }
See perldoc perlopentut, section "Filters"

Regards,
Randy.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: Modifying STDIN

2004-03-16 Thread John W. Krahn
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]
 




Re: Modifying STDIN

2004-03-16 Thread John McKown
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]
 




Re: Modifying STDIN

2004-03-16 Thread Randy W. Sims
On 3/16/2004 3:59 AM, James Taylor wrote:

I actually can't do it that way, this is a part of a custom perl module 
that someone wrote, and those are just 2 lines from the module.

On 03/16/04 00:06, 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.
Ok, I think I see what you want:  black magic

Try this:

#!/usr/bin/perl

package MySTDIN;

use strict;
use warnings;
use Tie::Handle;
our @ISA = qw(Tie::Handle);
sub TIEHANDLE {
  my $class = shift;
  open my $fh, '-';
  bless $fh, $class;
}
sub READLINE {
  my $self = shift;
  if (wantarray) {
my @input;
while (my $input = <$self>) {
  push @input, sprintf( "%3s %s", $., $input);
}
return @input;
  } else {
my $input = <$self>;
return undef unless defined $input;
return sprintf( "%3s %s", $., $input);
  }
}
package main;

use strict;
use warnings;
tie *STDIN, 'MySTDIN';
print ;
__END__

You just need to tie STDIN before it gets used, and alter the code in 
READLINE to alter the lines. Note you have to handle both scalar and 
array contexts.

I'm not completely sure of the correctness of the above. Use at your own 
risk.

Regards,
Randy.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 



Re: Modifying STDIN

2004-03-16 Thread Randy W. Sims
Try this:

#!/usr/bin/perl

package MySTDIN;

use strict;
use warnings;
use Tie::Handle;
our @ISA = qw(Tie::Handle);
Actually, you can remove the above two lines.

You just need to tie STDIN before it gets used, and alter the code in 
READLINE to alter the lines. Note you have to handle both scalar and 
array contexts.

I'm not completely sure of the correctness of the above. Use at your own 
risk.
And I think I've convinced myself that it is correct; anyone else?

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]