#!/usr/bin/perl -w use strict; use warnings; my @batch; my @batchinfo; my $done = 0; my $file; my $line; my $fh = *STDIN; my $prevfh; my $curIO = "STDIN"; my $prevIO; while (!$done) { print STDOUT "] "; $line = <$fh>; if (defined($line)) { print $line; if ($line =~ /B\s+([\w\.\/\-\_]+)/) { $prevfh = $fh; $prevIO = $curIO; $file = $1; print "INFO: Opening batch file $file\n"; if (open($fh, '<:encoding(UTF-8)', $file)) { push(@batch, $prevfh); push(@batchinfo, $prevIO); $curIO = $file; } else { warn "Could not open file '$file' $!"; $fh = $prevfh; $curIO = $prevIO; } } } else { # end of file, restore if any if (@batch) { $fh = pop(@batch); $curIO = pop(@batchinfo); print STDERR "INFO: Current IO: $curIO\n"; } else { die "No other input. Ending."; $done = 1; } } } #### a b c B tst2.bat 1 2 3 #### z y x w #### $ ./perl/tst.pl ] m m ] n n ] o o ] B tst.bat B tst.bat INFO: Opening batch file tst.bat ] a ] b ] c ] d ] e ] B tst2.bat INFO: Opening batch file tst2.bat ] 1 ] 2 ] 3 ] z ] y ] x ] w ] INFO: Current IO: tst.bat ] INFO: Current IO: STDIN ] No other input. Ending. at ./perl/tst.pl line 41, line 17.