Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by princepawn (Parson)
on Oct 02, 2007 at 14:59 UTC ( [id://642124]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on How to write a program to read stdin or command line?

Replies are listed 'Best First'.
Re: How to write a program to read stdin or command line?
by BrowserUk (Patriarch) 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 jdporter (Paladin) 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 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
        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.
        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 <>).
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 andreas1234567 (Vicar) on Oct 03, 2007 at 08:04 UTC
Re: How to write a program to read stdin or command line?
by ikegami (Patriarch) 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.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How to write a program to read stdin or command line?
by perlfan (Vicar) on Oct 02, 2007 at 18:48 UTC
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 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"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others examining the Monastery: (1)
As of 2024-04-19 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found