Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
XP is just a number
 
PerlMonks  

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

by princepawn (Parson)
on Oct 02, 2007 at 14:59 UTC ( #642124=perlquestion: print w/ replies, xml ) Need Help??
princepawn has asked for the wisdom of the Perl Monks concerning the following question:

How would you write a program which accepts input from the command line or STDIN and appends the word "world" to whatever input it is supplied?


Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

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

    Using your favorite text editor.

    Come on! This is homework! We don't mind helping people with homework if they say as much and ask for pointers. That's not what you did, so no bone for you.

      Come on! This is homework!
      Let's see how I can make a response to this unreasonable, unprovable assertion without being unreasonable or irrated.

      approach one: the personality appeal

      Well, it is not homework. If you knew who I were you would know that I am not in school.

      approach two: the common sense approach

      To me, the question asked just seems like a question asked. Is there something about this that sounds like homework? How do you distinguish a homework problem from a non-homework problem? You certainly were pretty confident of your assertion.

      I'm really rather disappointed in you. You have made some great posts in the time I have seen you here, and now you want to say something that (a) you cannot prove (b) is false.

      approach three: the prior evidence approach

      If you look at my home node you would see that I have a long long history of posting, and most of these posts show evidence of professional work... which does not preclude returning to school.

      the next approach: pure logic

      You cannot prove that this is homework. To do so would require evidence that you cannot find. I also cannot prove that this is not homework. To do so would require evidence that I cannot produce.

      the final approach: ignore him.

      He is making unfounded accusations. Of course, this hurts you in a few ways: (1) it led to a huge number of downvotes (2) it makes you look crooked (3) he might treat you like a person trolling for homework help in the future.

      All in all though, what can you do? It's a shame when someone tells a mistruth and has no evidence for it and then insults you in the process. But why stoop to his level? In terms of etiquette, he is acting poorly... he said "no bone for you" --- he's calling you a dog indirectly. He is out to humiliate you. His Perl skills are fantastic as he has shown in his posts. But that does not excuse his illegitimate and hateful attack on you.

      Seek a better company than he provides and hope that he will join you there someday.


      Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality
        Why are you so pissed of? Your question sounds like homework. And I rarely try to investigate the OPs biography before answering a question here.

        Homework: 3. preparatory reading or research

        The fact that you are a professional actually supports my point. It should have been dead easy for you to find how to check for parameters. Homework needs not be assigned by a school. It can be self-assigned, as it should have been in this case. Sounds like you could use a refresher.

        There was no malice in my reply. The idea was to influence others not to give an outright answer in order to influence the poster to actually do his own homework.

Re: How to write a program to read stdin or command line?
by svenXY (Deacon) on Oct 02, 2007 at 15:04 UTC
    Hi,
    $ perl -e 'while(true){print"line? >";$line=<STDIN>;chomp $line;print +$line," world\n"}' line? >line1 line1 world line? >sttw rtztrw sttw rtztrw world line? >last line.............. last line.............. world line? > world line? > $ perl -e 'print "@ARGV"," world\n" if $ARGV[0] || do {while(true){pri +nt"line? >";$line=<STDIN>;chomp $line;print $line," world\n"}}' "reve +rse(dlrow) is" reverse(dlrow) is world
    like that?
    update: adding support for "command-line" (interpreted as "arguments")
    Regards,
    svenXY
Re: How to write a program to read stdin or command line?
by jdporter (Canon) on Oct 02, 2007 at 15:06 UTC

    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

      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.

      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!

Re: How to write a program to read stdin or command line?
by BrowserUk (Pope) on Oct 02, 2007 at 16:10 UTC
      if that's what the OP means by "stdin or command line", you could also use -p instead of -n:
      perl -ple"$_ .= 'world'" file
        if that's what the OP means by "stdin or command line",

        Seems it was.

        you could also use -p instead of -n:

        You could, but I wouldn't as it is less flexible than -n.

        If you want to omit some lines from the output, you can't do that if you are also using -l


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How to write a program to read stdin or command line?
by runrig (Abbot) on Oct 02, 2007 at 16:51 UTC
    Need better specs. From you description I can't tell if there's no difference between the two (say, just use the '-p' flag on the command line or the <> operator in the code), or if maybe there is some difference where I might need to test @ARGV to see if there are any arguments before processing STDIN.
      Need better specs
      Shoot. Right you are. What I meant was "how would you emulate the way that grep receives its file argument. It could be via cat file | grep search_string or it could be via grep search_string file"


      Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality
        In that case I would just shift off any required arguments from @ARGV, then process the file list with <> (or manually with open, etc., if I were paranoid about using <>).
        This is built-in behavior in Perl. From perlrun:
        The null filehandle <> is special: it can be used to emulate the behavior of sed and awk. Input from <> comes either from standard input, or from each file listed on the command line. Here’s how it works: the first time <> is evaluated, the @ARGV array is checked, and if it is empty, $ARGV[0] is set to "-", which when opened gives you standard input. The @ARGV array is then processed as a list of filenames.
Re: How to write a program to read stdin or command line?
by princepawn (Parson) on Oct 02, 2007 at 17:34 UTC
    Ah File::Tools has an excellent suggestion of examples. His discussion of grep is just what you need.


    Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality
Re: How to write a program to read stdin or command line?
by perlfan (Deacon) on Oct 02, 2007 at 18:48 UTC
Re: How to write a program to read stdin or command line?
by lyklev (Pilgrim) on Oct 02, 2007 at 22:00 UTC
    Wow. Such a seemingly simple question, so much discussion.

    If you say you want to read from STDIN or command line, I guess you want it to behave like cat. I guess someone else needs to understand it too.

    while (<>) { chomp; print "$_ world\n"; }
Re: How to write a program to read stdin or command line?
by andreas1234567 (Vicar) on Oct 03, 2007 at 08:04 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2014-04-21 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (489 votes), past polls