Greetings those of you much smarter (and likely better looking) than I.

I'm trying to have a simple script that takes in commands both from the command line or from a batch file (if the command line directs) to pass to some lab equipment. The batch files can be nested (i.e. call other batch files.)

I've tried a simple testcase but it is not running as I desire/expect. It seems to process all of the filehandle input before starting the next.

Any suggestions? I could go full unix like with read, select etc., but I switched from C to PERL to make the user interface processing easier...

Thanks

<bold>Update:</bold>

Ken's answer does what I need and as most correct answers are, is simple and elegant. As for shift vs. pop, I definately want pop -effect- since I want a LIFO for the recursive batch processing. I say -effect- since as Ken's answer uses, recursion is a better tool for this problem.

Thanks to all who contemplated this for me.

#!/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; } } }

Sample input: tst.bat

a b c B tst2.bat 1 2 3

Sample input tst2.bat

z y x w

Example output:

$ ./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, <STDIN> line 17.

The output I want is for the z,y,x.w entries of tst2.bat to be processed before the 1,2,3 of tst.bat. Additionally when this finishes with the file inputs, it treats STDIN as if its at EOF, and exits indicating 'No other input', even though I have not pressed ctrl-D.

Thanks!

skorson

-- Steve

In reply to [SOLVED] Interleave STDIN and FILE IO by skorson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.