Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: How to write a program to read stdin or command line?

by jdporter (Canon)
on Oct 02, 2007 at 15:06 UTC ( #642132=note: print w/ replies, xml ) Need Help??


in reply to How to write a program to read stdin or command line?

I'd use Getopt::Long (or Getopt::Std, if that's your preference) to tell the program whether the input is on the commandline or not. One convention for this is to expect the input data on the commandline by default, but if a -i (--input) option is present, to read stdin instead.

A word spoken in Mind will reach its own level, in the objective world, by its own weight


Comment on Re: How to write a program to read stdin or command line?
Select or Download Code
Re^2: How to write a program to read stdin or command line?
by ikegami (Pope) on Oct 02, 2007 at 15:27 UTC

    I don't see why -i is required. If it's not on the command line, then it must be on STDIN, so there's no reason to use -i.

    my $input = @ARGV ? shift(@ARGV) : <STDIN>;

    That's the approach <ARGV> uses.

      Scalability / future expansion. The program may need other data on the command line unrelated to the need stated in the OP.

      Anyway, I didn't say -i is required. I answered the question "How would you...." And this is how I would. If you would do it otherwise, fine.

Re^2: How to write a program to read stdin or command line?
by naikonta (Curate) on Oct 03, 2007 at 07:13 UTC
    I like the flexibility of Getopt::Long and simplicity of Getopt::Std. For even simpler and quick solution, I much prefer the -s switch.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://642132]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-12-28 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (180 votes), past polls