STDIN as well as command line input
How do I get a Perl program to accept input from STDIN as well as command line input. I have a program (foo.pl) that is designed to read the contents of @ARGV and process each item in the array. Tastes great. Less filling. So, when I do something like this, things work just fine: %foo.pl a b c d e f g I have another program (bar.pl) that prints to STDOUT. The output is the same sort of data needed by foo.pl. So, I thought I'd give this is a whirl: %bar.pl | foo.pl But alas, foo.pl never seems to get the input sent from bar.pl. It does not seem to read from STDIN. What should I do to my first program (foo.pl) so it can accept command line input as well as input from STDIN? -- Eric Lease Morgan (574) 631-8604
Re: STDIN as well as command line input
On Mon, Apr 26, 2004 at 10:14:51AM -0500, Eric Lease Morgan ([EMAIL PROTECTED]) wrote: What should I do to my first program (foo.pl) so it can accept command line input as well as input from STDIN? How are you reading from the files? Opening them yourself one at a time? Don't. Use the magic filehandle. See my talk A Field Guide To The Perl Command Line on http://petdance.com/perl/. It was also an article in The Perl Journal a few months back. I'll be presenting the talk at YAPC this year. http://www.yapc.org/America/ xoa -- Andy Lester = [EMAIL PROTECTED] = www.petdance.com = AIM:petdance
Re: STDIN as well as command line input
On Mon, Apr 26, 2004 at 10:14:51AM -0500, Eric Lease Morgan wrote: %bar.pl | foo.pl But alas, foo.pl never seems to get the input sent from bar.pl. It does not seem to read from STDIN. What should I do to my first program (foo.pl) so it can accept command line input as well as input from STDIN? Try using the magic filehandle. So in foo.pl : while ( defined( $line = ) ) { ... } The magic filehandle will read stuff from @ARGV and will also read from STDIN. //Ed -- Eric Lease Morgan (574) 631-8604 -- Ed Summers aim: inkdroid web: http://www.inkdroid.org We act as though comfort and luxury were the chief requirements of life, when all that we need to make us happy is something to be enthusiastic about. [Einstein]
Re: STDIN as well as command line input
Eric Lease Morgan wrote: How do I get a Perl program to accept input from STDIN as well as command line input. I have a program (foo.pl) that is designed to read the contents of @ARGV and process each item in the array. Tastes great. Less filling. So, when I do something like this, things work just fine: %foo.pl a b c d e f g I have another program (bar.pl) that prints to STDOUT. The output is the same sort of data needed by foo.pl. So, I thought I'd give this is a whirl: %bar.pl | foo.pl But alas, foo.pl never seems to get the input sent from bar.pl. It does not seem to read from STDIN. This sort of situation can be dealt with with back ticks: foo.pl `bar.pl` This is nice in that you can probably do this too: foo.pl a b c `bar.pl` d e f g h `bar.pl x y z` i j k A popular GNUism might be helpful here as well. Many GNU programs use an option command line argument of -- to indicate that input should be taken from STDIN instead of from other command line arguments. -- Michael McDonnell, GCIA Winterstorm Solutions, Inc. [EMAIL PROTECTED]
Re: STDIN as well as command line input
On Apr 26, 2004, at 10:43 AM, Andy Lester wrote: How are you reading from the files? Opening them yourself one at a time? Don't. Use the magic filehandle. On Apr 26, 2004, at 10:44 AM, Dennis Boone wrote: If your perl script is structured like this: while () { # process } then perl will process stdin if no files are named, or the contents of each file named on the command line in sequence. Alas, my inputs are not the names of files. They are scalars, like this: plato-cratylus-1072532262 plato-charmides-1072462708 bacon-new-1072751992 -- Eric (574) 631-8604
Re: STDIN as well as command line input
On Apr 26, 2004, at 10:53 AM, Michael McDonnell wrote: This sort of situation can be dealt with with back ticks: foo.pl `bar.pl` This is nice in that you can probably do this too: foo.pl a b c `bar.pl` d e f g h `bar.pl x y z` i j k A popular GNUism might be helpful here as well. Many GNU programs use an option command line argument of -- to indicate that input should be taken from STDIN instead of from other command line arguments. The back ticks solutions works well. Thank you. I will see about modifying my code to get smart about -- arguments. Again, thanks. -- Eric (574) 631-8604
Re: STDIN as well as command line input
Michael McDonnell wrote: A popular GNUism might be helpful here as well. Many GNU programs use an option command line argument of -- to indicate that input should be taken from STDIN instead of from other command line arguments. '--' is usually used to mean that anything that follows is a non-option argument. For example, to remove a file called '-rf', you could use: rm -- -rf to force rm to interpret '-r' as a filename instead of a pair of options. '-' is usually used as a placeholder for STDIN. Unix programs generally read from STDIN if no files are specified on the command line. If one or more files are specified on the command line, '-' is usually taken to mean STDIN, so cat foo - bar baz would list the contents of foo, then baz, then bar. But what Eric seems to want to do is supply *data* either from a stream on STDIN or directly on the command line. This is a little unconventional, and you have to remember that the data will have to be parsed differently depending on where it comes from. For example, foo.pl a b c d e produces 5 elements in @ARGV, while echo a b c d e | foo.pl produces one element (with a newline appended) that has to be parsed and split into elements at the correct places. This can get messy if you have escape characters, whitespace as data, and so on. I would suggest that the standard way to do this sort of thing would be to read *all* data from STDIN (and, optionally, from files specified on the command line) and treat each line as one record, if your data never contains newlines. If you had some data you wanted to pass manually to the program, you could always invoke it as: foo.pl plato-cratylus-1072532262 plato-charmides-1072462708 bacon-new-1072751992 ^D or echo plato-cratylus-1072532262 plato-charmides-1072462708 bacon-new-1072751992 | foo.pl Either that, or supply all data on the command line, and use the backticks method as Michael suggests. Mixing the two conventions might lead to confusion later on, especially if someone else will end up having to use the program. -- William Wueppelmann Electronic Systems Specialist Canadian Institute for Historical Microreproductions (CIHM) http://www.canadiana.org/