how do I choose between an explicit file and standard input

L

Lowell Kirsh

I want to write a script that will take an optional filename as a
command line argument and will use stdin if no parameter is supplied.
What the script does is count the number of unique lines in a file. For
example I want 'theScript foo' to act like:

system "uniq < foo | wc -l"

but if no parameter is supplied it should act like:

system "uniq < {standard input} | wc -l"

Does this make sense?

Lowell
 
G

gnari

Lowell Kirsh said:
I want to write a script that will take an optional filename as a
command line argument and will use stdin if no parameter is supplied.
What the script does is count the number of unique lines in a file. For
example I want 'theScript foo' to act like:

system "uniq < foo | wc -l"

but if no parameter is supplied it should act like:

system "uniq < {standard input} | wc -l"

the general answer to this is <>
usually in the form:
while (<>) {
# do stuff, like $c{$_}++;
}
# wrap up, like print scalar keys(%c),"\n";

in this particular case, you might want to use -n:
#/usr/bin/perl -nl
$c{$_}++;
END {print scalar keys %c}

or a command line:
perl -nle'$c{$_}++;END{print scalar keys %c}' input_file
another_input_file
perl -nle'$c{$_}++;END{print scalar keys %c}' < input_file
grep bla somefile | perl -nle'$c{$_}++;END{print scalar keys %c}'

gnari
 
A

Anno Siegel

Lowell Kirsh said:
I want to write a script that will take an optional filename as a
command line argument and will use stdin if no parameter is supplied.
What the script does is count the number of unique lines in a file. For
example I want 'theScript foo' to act like:

system "uniq < foo | wc -l"

but if no parameter is supplied it should act like:

system "uniq < {standard input} | wc -l"

Does this make sense?

It makes so much sense that Perl makes this the default behavior of
the input operator "<>" if no file handle is given. It even allows you
to specify more than one file on the command line, which are read one
after the other. If none is given, it reads from STDIN.

So your program could be written like this:

my %h;
@h{ <> } = ();
print scalar keys %h, "\n";

Anno
 
L

Lowell Kirsh

thanks.

Anno said:
It makes so much sense that Perl makes this the default behavior of
the input operator "<>" if no file handle is given. It even allows you
to specify more than one file on the command line, which are read one
after the other. If none is given, it reads from STDIN.

So your program could be written like this:

my %h;
@h{ <> } = ();
print scalar keys %h, "\n";

Anno
 
C

ctcgag

Lowell Kirsh said:
I want to write a script that will take an optional filename as a
command line argument and will use stdin if no parameter is supplied.
What the script does is count the number of unique lines in a file. For
example I want 'theScript foo' to act like:

system "uniq < foo | wc -l"

but if no parameter is supplied it should act like:

system "uniq < {standard input} | wc -l"

Does this make sense?

You use the empty read operator, <>.

One caveat is that your program should not invoke <> after the first
time it returns undef (which if all you want is wc knock-off, should
not be a problem.)

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top