Joel G Mathew
Joel G Mathew

Reputation: 8091

How do I reset data from STDIN when I am finished with it?

I have a program while gets data from a pipe of the contents of a test file (cat file). I typically use the diamond operator to get the data line by line from STDIN, till the last line. Trouble is that I would like to reuse the same data for more than one subroutine. How is it possible to reset reading of STDIN so that the data can be read again from the first line?

sub downloadsrt {
    print "Printing list of subtitle files in downloadable form..\n";
    while (<>) {
        chomp($_);
        (my $fname,my $path, my $suffix) = fileparse($_);
        $_=$fname;
        my ($name, $ext) = $fname =~ /(.*)\.(.*)/;  
        #For srt
        my $newfile=$path.$name.".$ext";
        $newfile =~ s/\s/%20/g; 
        $newfile =~ s/\/root/http:\/\/$localip/;
        print $newfile."\n";
    }
}
sub dummysub {
    while (<>) {
        // Something else
    }
}

downloadsrt;
dummysub;

I would like to access STDIN using multiple subroutines, and each should get data from the first line of STDIN.

Unless I am mistaken this is not lexically scoped.

Upvotes: 0

Views: 205

Answers (1)

Pierre
Pierre

Reputation: 1246

As suggested by @mpapec, you need to store the data from STDIN into an array:

my @stdin = <>;
mysub_1(\@stdin);
mysub_2(\@stdin);

Upvotes: 1

Related Questions