How can I open STDIN <> in perl?

How can I open STDIN a second time?

Let's say I have this data

John Smith  25  O   ATG 180
Peter Jackson   40  AB  GGTA    173
Carl Anderson   32  A   GGT 172

      

And I have this code

while( my $line = <>)  {
    print $line;
}
while( my $line = <>)  {
    print $line;
}

      

I want it to print

John Smith  25  O   ATG 180
Peter Jackson   40  AB  GGTA    173
Carl Anderson   32  A   GGT 172
John Smith  25  O   ATG 180
Peter Jackson   40  AB  GGTA    173
Carl Anderson   32  A   GGT 172

      

+3


source share


2 answers


You cannot, you have emptied the stream. If you want to use it again, put the strings in an array.

my @lines;
while( my $line = <>)  {
    print $line;
    push @lines, line;
}

foreach my $line (@lines)  {
    print $line;
}

      



Or write a file and get filefile in file.

+9


source


If the STDIN is attached to a file you can seek(STDIN, 0, 0)

, but in general you cannot do what you ask.



+5


source







All Articles