Am Mittwoch, 13. April 2005 10.46 schrieb Ramprasad A Padmanabhan: > On Wed, 2005-04-13 at 12:32, Thomas BÃtzler wrote: > > Ramprasad A Padmanabhan <[EMAIL PROTECTED]> asked: > > > I want to write a perl script like "gnu less". > > > > > > My perl script accepts input on STDIN or filename(s) as arguments. > > > If both are missing it should print the error message. How do > > > I do this ? > > > > There is always input on STDIN - even a straight eof could be input, > > like in "cat empty_file | ramprasads_less". You should probably try > > and check for filename arguments and revert to STDIN processing if > > there are none, i.e. > > > > my @files; > > > > if( @ARGV ){ > > @files = @ARGV; > > } else { > > @files = ("-"); > > } > > > > foreach my $file (@files){ > > # transparent gzip decompression > > $file = "/usr/bin/gzip -d < $file|" if $file =~ m/\.gz$/; > > > > open( IN, $file ) or die "Cam't open '$file': $!"; > > # do something > > close( IN ); > > } > > > > HTH, > > Thomas > > That is perfectly ok .. but I require to print usage if there is no > input How do I do this ? > > Thanks > Ram
The following code abbreviates the STDIN/filename issue, but of course the assignement to $show_usage within the loop (for every input line) is not optimal... The script can be called by $ script filename1 filename2 or $ script < filename1 or simply (but rather senseless) $ script === #!usr/bin/perl use warnings; use strict; my $show_usage=1; while (<>) { $show_usage=0; # do something with the lines of the content # provided by filenames or direct input from stdin # Lines are provided in $_ . } usage if $show_usage; sub usage { print <<EOF The usage here EOF } === > > > ---------------------------------------------------------- > Netcore Solutions Pvt. Ltd. > Website: http://www.netcore.co.in > Spamtraps: http://cleanmail.netcore.co.in/directory.html > ---------------------------------------------------------- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>