Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • #!/usr/bin/perl -w
    use strict;

    die "Usage: $0 questions.txt [morequestions.txt ...] > answers.txt"
        if not @ARGV;

    while( <> ) {
        print STDERR $_;
        print $_;
        print scalar <STDIN>;
    }

    The key is that the question files must be given as commandline arguments, not passed in via redirection. The <> diamond operator will read from the files listed in @ARGV and you can read STDIN separately from it at the same time.

    • Ah, much more in line with what I would probably do, however not as short and artistic.

      Thanks for the solution it is very nice.
    • Yea, this is a suitable solution for

      foo.pl inputfile.txt

      But what if you want to do

      grep bar inputfile.txt | foo.pl

      instead?

      --
      Ordinary morality is for ordinary people. -- Aleister Crowley
      • If you use bash, you can use process substitution instead:

        foo.pl <( grep bar inputfile.txt )

        • Far too complicated :)
          --
          Ordinary morality is for ordinary people. -- Aleister Crowley
          • It’s more reliable (/dev/tty isn’t always available) and more flexible (you don’t need Expect to feed it answers from a file, you just redirect standard input). But the clincher for me is that it’s still less and simpler code. Cf. Carter’s Compass.

            • Well, that's a borderline. IMO the API should always be nicer than the guts :)
              --
              Ordinary morality is for ordinary people. -- Aleister Crowley