SubbaReddy M wrote:

> Hello Gurus,
> Source code: of printFile.pl
> #########   File: printFile.pl  ##################
> #! /usr/bin/perl -w
> my (@data, $user);
> 
> # Input rediretion file
> 
> $user = (defined @ARGV) ? shift @ARGV : "Anonymous";
> 
> # I don't wish to prompt by program, but it's still waiting how to Step Over
> here
> @data = <STDIN> if ( defined <STDIN>);
> 
> die  " perl $0 < data.txt" if (@data);
> 
> print "User: $user \n";
> foreach (@data){
>                             print $_;
> }
> 
> 1;
> ########### EOF #############
> 
> 
> 
> I have a perl scripts, which expects the input file as redirection at
> command line.
> i.e.,
> [user3@Linux] #  perl printFile.pl  < data.txt
> 
> => This will display the inputed file content, if file name is piped as
> redirection.
> => Otherwise, program will wait to accept the file.
> => So, I don't want program prompt for the file name,
> => I would like to check the file name is given or not.
> => But, becuase of <STDIN> statement in program to dump the content of the
> file into Array, will cause to wait if  " < data.txt" is missing at command
> line.
> => It's very easy, if @ARGV checking, will have the command line parameters.
> But, it 's not in command line paramenters list.
> => It is input redirection.
> 
> <=> How to check the input direction is given or not in perl, @ = <STDIN>;
> => like @ = <STDIN> if ( defined <STDIN> );   But this will not full fill
> the requirement, because, it's prompting
> for input, if redirection missing at commandline.
> 
> Please, kindly give me suggestions to proceed.


Either of these two methods or both should work:

use strict;

my $user = (defined @ARGV) ? shift @ARGV : "Anonymous";

# Method 1: if STDIN is a tty

if (-t STDIN) {
        die  "perl $0 < data.txt";
}

# Method 2: checking for file

if (-f STDIN) {

        my @data = <STDIN>;
        print "User: $user\n";
        foreach (@data) {
                print $_;
        }
}

__END__


PS: You're using excessive cross-posting.  Keep it to the single most
appropriate group.
-- 
   ,-/-  __      _  _         $Bill Luebkert   ICQ=14439852
  (_/   /  )    // //       DBE Collectibles   Mailto:[EMAIL PROTECTED]
   / ) /--<  o // //      http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/

_______________________________________________
Perl-Unix-Users mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to