H
Henry Law
(Googled a lot for this without success; a good search string eluded
me).
I have a Perl program running under ActiveState Perl which needs to
read in a number of control statements before it gets to work. I want
to be able to specify the name of the file containing the control
statements (myprog.pl -f filename), but have it default to STDIN if no
name is supplied (myprog.pl). I can't find a neat way of coding it.
This works (aside from an ActiveState problem I'll come to):
#! C:\Perl\bin\Perl.exe
use strict;
use warnings;
use Getopt::Std;
our $opt_f;
getopts('f:');
my @control_commands;
if (defined $opt_f) {
open (COMMANDS,$opt_f) or
die "Couldn't open command file '$opt_f'$!";
@control_commands = <COMMANDS>;
} else {
print "Enter control commands, Ctrl-D to finish:";
@control_commands = <STDIN>; # See problem described later
}
print for (@control_commands);
__END__
But it requires me to slurp the whole control file in at one go; there
won't ever be more than a dozen or so statements so it'll work OK, but
it's inelegant. What I'd like to do is (lapsing into pseudocode here
and there as I really can't work out how to code this ...)
if (defined $opt_f) {
open ($file_handle,$opt_f) or
die "Couldn't open command file '$opt_f'$!";
} else {
open ($file_handle,STDIN); # doesn't compile
}
while (my $command = <$file_handle> ) {
do_stuff_with_single_command;
}
.... there must be a "usual" way of doing this; could someone point me
in the right direction?
In passing, I can't find the ActiveState equivalent of Ctrl-D to
terminate STDIN - and it's not in the html manual as far as I can see.
Can someone help with that?
me).
I have a Perl program running under ActiveState Perl which needs to
read in a number of control statements before it gets to work. I want
to be able to specify the name of the file containing the control
statements (myprog.pl -f filename), but have it default to STDIN if no
name is supplied (myprog.pl). I can't find a neat way of coding it.
This works (aside from an ActiveState problem I'll come to):
#! C:\Perl\bin\Perl.exe
use strict;
use warnings;
use Getopt::Std;
our $opt_f;
getopts('f:');
my @control_commands;
if (defined $opt_f) {
open (COMMANDS,$opt_f) or
die "Couldn't open command file '$opt_f'$!";
@control_commands = <COMMANDS>;
} else {
print "Enter control commands, Ctrl-D to finish:";
@control_commands = <STDIN>; # See problem described later
}
print for (@control_commands);
__END__
But it requires me to slurp the whole control file in at one go; there
won't ever be more than a dozen or so statements so it'll work OK, but
it's inelegant. What I'd like to do is (lapsing into pseudocode here
and there as I really can't work out how to code this ...)
if (defined $opt_f) {
open ($file_handle,$opt_f) or
die "Couldn't open command file '$opt_f'$!";
} else {
open ($file_handle,STDIN); # doesn't compile
}
while (my $command = <$file_handle> ) {
do_stuff_with_single_command;
}
.... there must be a "usual" way of doing this; could someone point me
in the right direction?
In passing, I can't find the ActiveState equivalent of Ctrl-D to
terminate STDIN - and it's not in the html manual as far as I can see.
Can someone help with that?