Re: How to write a program to read stdin or command line?
by BrowserUk (Patriarch) on Oct 02, 2007 at 16:10 UTC
|
perl -nle"print $_ . 'world'" file
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.
| [reply] [d/l] |
|
if that's what the OP means by "stdin or command line", you could also use -p instead of -n:
perl -ple"$_ .= 'world'" file
| [reply] [d/l] [select] |
|
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.
| [reply] [d/l] [select] |
|
|
|
|
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
| [reply] [d/l] [select] |
|
my $input = @ARGV ? shift(@ARGV) : <STDIN>;
That's the approach <ARGV> uses. | [reply] [d/l] [select] |
|
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.
| [reply] [d/l] |
|
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!
| [reply] [d/l] |
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. | [reply] [d/l] |
|
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
| [reply] [d/l] [select] |
|
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.
| [reply] |
|
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 <>).
| [reply] [d/l] [select] |
|
Re: How to write a program to read stdin or command line?
by svenXY (Deacon) on Oct 02, 2007 at 15:04 UTC
|
$ 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 | [reply] [d/l] |
Re: How to write a program to read stdin or command line?
by andreas1234567 (Vicar) on Oct 03, 2007 at 08:04 UTC
|
Chapter 14 of Perl Best Practices, Command-Line Processing recommends using tools like
The two last modules are written by the book's author.
| [reply] |
|
| [reply] |
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.
| [reply] |
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
|
| [reply] |
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
| [reply] |
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";
}
| [reply] [d/l] [select] |