Can you undo a read from a file

T

Tim Greenwood

Is it possible (and preferably simple) to undo a line input from STDIN
or any file handle? I want to do something like


$line = <STDIN>;
if ($line =~ $my_pattern) {
# process it all
} else {
# push the line back to be read by some other part of the program
}
 
D

David Efflandt

Is it possible (and preferably simple) to undo a line input from STDIN
or any file handle? I want to do something like


$line = <STDIN>;
if ($line =~ $my_pattern) {
# process it all
} else {
# push the line back to be read by some other part of the program
}

See 'perldoc -f seek' (and 'perldoc -f tell') which should work for a real
file. You cannot expect to turn back steams like STDIN, pipes, fifos,
sockets.
 
M

Matt Garrish

Tim Greenwood said:
Is it possible (and preferably simple) to undo a line input from STDIN
or any file handle? I want to do something like


$line = <STDIN>;
if ($line =~ $my_pattern) {
# process it all
} else {
# push the line back to be read by some other part of the program
}

I don't follow what you mean by pushing back a line read from stdin. Print
it back to stdout? To what end if your program is going to process it
anyway?

Are you just looking for an array?

my @process_later;

my $line = <STDIN>;

if ($line =~ /my_pattern/) { process $line }
else { push @process_later, $line; }

Reading from a file would be easier. You could still use an array if you
don't have a lot of data, or seek back to the beginning of the file and
reprocess it, or use a temp file, etc. etc.

Natt
 
D

Dave Weaver

Is it possible (and preferably simple) to undo a line input from STDIN
or any file handle? I want to do something like


$line = <STDIN>;
if ($line =~ $my_pattern) {
# process it all
} else {
# push the line back to be read by some other part of the program
}


Perhaps something like this? :

#!/usr/bin/perl
use warnings;
use strict;

{{

my %buffer;

# Read a line from the given file handle
sub getline {
my ($fh) = @_;

if (exists $buffer{$fh} and @{$buffer{$fh}}) {
return shift @{$buffer{$fh}};
}
return scalar <$fh>;
}

# "Push back" a line to the given filehandle, so that
# it will be returned on the next getline() call.
sub ungetline {
my ($fh, $line) = @_;
push @{$buffer{$fh}}, $line;
}

}}

my $in = getline(\*STDIN);
print "Got: $in";
ungetline(\*STDIN, "One\n");
ungetline(\*STDIN, $in);
$in = getline(\*STDIN);
print "Got: $in";
$in = getline(\*STDIN);
print "Got: $in";
__END__

[not _fully_ tested]

Cheers,
Dave
 
W

Web Surfer

[This followup was posted to comp.lang.perl.misc]

Is it possible (and preferably simple) to undo a line input from STDIN
or any file handle? I want to do something like


$line = <STDIN>;
if ($line =~ $my_pattern) {
# process it all
} else {
# push the line back to be read by some other part of the program
}

You can use the "seek" function to "reposition" a file's "current I/O
location". See "perldoc -f seek" for further details.
 
A

A. Sinan Unur

[This followup was posted to comp.lang.perl.misc]

Hmmmm ... I am reading this in comp.lang.perl.misc. Why do I need to be
told that it was posted here?
You can use the "seek" function to "reposition" a file's "current I/O
location". See "perldoc -f seek" for further details.

I would like to see how you that with STDIN.
 
T

Tim Greenwood

Thanks for the suggestions. I am a complete beginner with perl. Since
I do not have a lot of data my best bet is perhaps to just read it all
into an array. (Which I thought of just after pressing send on the
original submission!).

TIm
 
B

Ben Morrow

Is it possible (and preferably simple) to undo a line input from STDIN
or any file handle? I want to do something like

If you're using 5.8 and have a perl built with PerlIO (you probably
have if it's 5.8), you can use my module IO::Unread. It uses
Inline::C, and due to issues with Inline::MakeMaker you will need to
install at least Inline first: CPAN.pm will probably install Inline::C
for you.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top