Read from a pipe or die in perl

I have a perl program that I want to read from a file passed as an argument or from a pipe. If their is no pipe or arguments, I want it to output a help message. I am stuck on how to prevent perl from reading from the keyboard if it isn't fed any file names or data from a pipe. The only things I can find by searching are tutorials about how to read from files and use the diamond operator.

Here are the relevant lines of the program:

if ($ARGV[0] eq "--help" || $ARGV[0] eq "-h")
{
        &help;
}     

if ( defined($ARGV[0]))
{
         #fill array for sorting
}
else
{
                foreach(<>)
                my @splitter = split(/\s+/);
                foreach(@splitter)
                {
                        if (!/^[0-9]+$/)
                        {
                        die("I only like numbers!\n");
                        }
                        push (@file, $_);
                }

# STDIN and/or file contents get stored in the array @file
if ( ! defined(@file))
{
        &help;
}

Hi,

Try this...

if (! defined $ARGV[0] || $ARGV[0] eq "--help" || $ARGV[0] eq "-h")
{
        &help;
}

Thanks pravin27. Unfortunately that didn't solve my problem. Adding the "! defined()" stops it from reading from a pipe if no file name is passed to it. Is there anyway to detect if a shell has redirected STDIN?

Hi,

Please correct me if i am wrong .You mean to say if there is no command line argument then script should ask for the filename ?

how you are running this script ?

If there is no command line argument and if STDIN is not redirected (file or pipe), the program should exit and output a usage message. The script is meant to behave like a normal UNIX command line utility. I appreciate you taking the time to help pravin27.