Re: Reading from Pipe and Command Line
On Wed, Jun 25, 2003 at 01:09:41PM -0700, Jeff Westman wrote: > Could someone help me please? > > I am trying to write a simple script that will take input from the command > line as well as input from a pipe. > > For example, the script should be able to do both of the following: > > $ cat someFile | myPerlScript.pl # from a pipe > > and > > $ myPerlScript.pl someFile # from command line > > This is what I have (very simple): > > #--- (begin) # > #-- myScript.pl --# > # > #!/bin/perl > use warnings; > > sub parseFile() > { >while (<>) { ## I tried passing in \*STDIN or \*F but > ## had nothing but problems with that > > # do some processing to the file > # ... > print ". "; ## just to do something in the loop for now > } > } > > if (@ARGV) { > $file = shift; > open(F, "< $file") or die "cannot open file $file: $!\n"; > parseFile; > close(F); > } > else { > parseFile; > } > #--- (end) # You are working far too hard. Remove most of your code: #!/bin/perl use warnings; while (<>) { # do some processing to the file # ... print ". "; ## just to do something in the loop for now } > What is obvious to one is not always obvious to another. Quite. -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Reading from Pipe and Command Line
Jeff Westman wrote: > > Could someone help me please? > > I am trying to write a simple script that will take input from the command > line as well as input from a pipe. > > For example, the script should be able to do both of the following: > > $ cat someFile | myPerlScript.pl # from a pipe > > and > > $ myPerlScript.pl someFile # from command line > > This is what I have (very simple): > > #--- (begin) # > #-- myScript.pl --# > # > #!/bin/perl > use warnings; > > sub parseFile() > { >while (<>) { ## I tried passing in \*STDIN or \*F but > ## had nothing but problems with that > > # do some processing to the file > # ... > print ". "; ## just to do something in the loop for now > } > } > > if (@ARGV) { > $file = shift; <> will open open and read through each line of the files listed in @ARGV but you are removing the file names from @ARGV. > open(F, "< $file") or die "cannot open file $file: $!\n"; > parseFile; > close(F); > } > else { > parseFile; > } > #--- (end) # sub parseFile() { while (<>) { ## I tried passing in \*STDIN or \*F but ## had nothing but problems with that # do some processing to the file # ... print ". "; ## just to do something in the loop for now } } if (@ARGV) { parseFile; } else { parseFile; } Or just: sub parseFile() { while (<>) { ## I tried passing in \*STDIN or \*F but ## had nothing but problems with that # do some processing to the file # ... print ". "; ## just to do something in the loop for now } } parseFile; > What is obvious to one is not always obvious to another. Obviously. :-) John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Reading from Pipe and Command Line
--- Paul Johnson <[EMAIL PROTECTED]> wrote: > On Wed, Jun 25, 2003 at 01:09:41PM -0700, Jeff Westman wrote: > > > Could someone help me please? > > > > I am trying to write a simple script that will take input from the > command > > line as well as input from a pipe. > > > > For example, the script should be able to do both of the following: > > > > $ cat someFile | myPerlScript.pl # from a pipe > > > > and > > > > $ myPerlScript.pl someFile # from command line > > > > This is what I have (very simple): > > > > #--- (begin) # > > #-- myScript.pl --# > > # > > #!/bin/perl > > use warnings; > > > > sub parseFile() > > { > >while (<>) { ## I tried passing in \*STDIN or \*F but > > ## had nothing but problems with that > > > > # do some processing to the file > > # ... > > print ". "; ## just to do something in the loop for now > > } > > } > > > > if (@ARGV) { > > $file = shift; > > open(F, "< $file") or die "cannot open file $file: $!\n"; > > parseFile; > > close(F); > > } > > else { > > parseFile; > > } > > #--- (end) # > > You are working far too hard. Remove most of your code: > > #!/bin/perl > use warnings; > > while (<>) { > # do some processing to the file > # ... > print ". "; ## just to do something in the loop for now > } > > > What is obvious to one is not always obvious to another. > > Quite. WAY too simple! I finally got it to work (see below) but obviously the easy solution is the best solution!!! #!/bin/perl use warnings; sub parseFile(*) { $fh = shift; while (<$fh>) { # do some processing to the file # ... print ". "; } } if (@ARGV) { $file = shift; open(F, "< $file") or die "cannot open file $file: $!\n"; parseFile(\*F); close(F); } else { parseFile(\*STDIN); } Thanks for the help!! JW __ Do you Yahoo!? SBC Yahoo! DSL - Now only $29.95 per month! http://sbc.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Reading from Pipe and Command Line
In article <[EMAIL PROTECTED]>, Jeff Westman wrote: > Could someone help me please? > > I am trying to write a simple script that will take input from the command > line as well as input from a pipe. > > For example, the script should be able to do both of the following: > > $ cat someFile | myPerlScript.pl # from a pipe [...] I think this Q was just post here. What about (using diamond '<>')... #!/usr/bin/perl use warnings; use strict; print "Reading from ", $ARGV[0] || "a pipe", ":\n"; while (<>) { print $_; } -- Kevin Pfeiffer International University Bremen -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Reading from Pipe and Command Line
> Could someone help me please? > > I am trying to write a simple script that will take input > from the command line as well as input from a pipe. > > For example, the script should be able to do both of the following: > > $ cat someFile | myPerlScript.pl # from a pipe > > and > > $ myPerlScript.pl someFile # from command line > > This is what I have (very simple): > > #--- (begin) # > #-- myScript.pl --# > # > #!/bin/perl > use warnings; > > sub parseFile() > { >while (<>) { ## I tried passing in \*STDIN or \*F but > ## had nothing but problems with that > > # do some processing to the file > # ... > print ". "; ## just to do something in the loop for now > } > } > > if (@ARGV) { > $file = shift; > open(F, "< $file") or die "cannot open file $file: $!\n"; > parseFile; You need to give parseFile() something to parse, Perhaps open(F, ); parseFile(*F); close(F); Here and parseFile(*STDIN); Then in parseFile() just do your while() on the FH passed to it. That I'm not 100% how to do. Also use strict; That will help tracj down probs. HTH DMuey > close(F); > } > else { > parseFile; > } > #--- (end) # -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Reading from Pipe and Command Line
Could someone help me please? I am trying to write a simple script that will take input from the command line as well as input from a pipe. For example, the script should be able to do both of the following: $ cat someFile | myPerlScript.pl # from a pipe and $ myPerlScript.pl someFile # from command line This is what I have (very simple): #--- (begin) # #-- myScript.pl --# # #!/bin/perl use warnings; sub parseFile() { while (<>) { ## I tried passing in \*STDIN or \*F but ## had nothing but problems with that # do some processing to the file # ... print ". "; ## just to do something in the loop for now } } if (@ARGV) { $file = shift; open(F, "< $file") or die "cannot open file $file: $!\n"; parseFile; close(F); } else { parseFile; } #--- (end) # What is obvious to one is not always obvious to another. Thanks, JW __ Do you Yahoo!? SBC Yahoo! DSL - Now only $29.95 per month! http://sbc.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]