chinamox has asked for the wisdom of the Perl Monks concerning the following question:

Hello again monks,

I have finished writing a program that requires <STDIN> input to function correctly. Because of this, I want the program to display a small error message when the user provides on file for the program.

For example:

username$: perl myprog.pl /path/path/file

results in the program executing as expected using the data in file. However I want this:

username$: perl myprog.pl

to cause the print out of a message simlar to this:

Error! plese try: 'perl myprog.pl /some/filename'

can this be done easly through an if-else statement? If so how should I check to see if <> is being used or not?

Thank you very much for your time.

-mox

Replies are listed 'Best First'.
Re: Returning an error message when <STDIN> is not in use
by davorg (Chancellor) on Nov 09, 2006 at 15:54 UTC

    I'm not at all sure what this has to do with STDIN, but it sounds like you just want to count the number of arguments passed to your program.

    unless (@ARGV) { # error }

    If I'm misunderstanding and you want to check whether STDIN is connected to the console, then you can use the -t file test operator.

    if (-t STDIN) { # STDIN is connected to the console }
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      I'm not entirely certain, but I think he's referring to the magic behavior of the <> operator when you don't give it a filehandle. This is not STDIN as such, but it's similarly magic (insofar as it allows reading from a filehandle without opening it first), and it makes a certain amount of sense in conjunction with what he was saying about arguments.


      Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. You can just call me "Mister Sanity". Why, I've got so much sanity it's driving me crazy.

      Thank you for your response. The unless (@ARGV) statement worked great!

      -mox
Re: Returning an error message when <STDIN> is not in use
by ikegami (Patriarch) on Nov 09, 2006 at 15:49 UTC
Re: Returning an error message when <STDIN> is not in use
by ptum (Priest) on Nov 09, 2006 at 15:52 UTC

    For something like this, I would probably inspect the @ARGV array and call a usage() subroutine if:

    • @ARGV was empty
    • a file test like '-e' failed on the first element of @ARGV
    • I was unable to open the file specified in @ARGV

    Update: Dang! Beat to the punch by ikegami! :)

    A sample usage() subroutine might look like this:

    sub usage { die "You must specify an input file.\nUsage: $0 <file_name>\n"; }
Re: Returning an error message when <STDIN> is not in use
by wjw (Priest) on Nov 09, 2006 at 15:57 UTC
    #!/usr/bin/perl -w use strict; if (scalar(@ARGV) < 1) { print "Usage: perl myprog.pl (path_to_file)\n"; exit; } ... rest of perl program

    ...the majority is always wrong, and always the last to know about it...