How to read input data from pipe, file and files

M

maasha

G'day all,

In order to stream-line my scripts I would like to be able to read
data from either file(s) or a pipe, however, the way I do this results
in a lot of redundant code as shown in the below code snip. Somehow,
there must be a smarter way to do this!?!

Cheers,

M


if ( not -t STDIN )
{
$fh = &Common::pipe_open();

print &count_entries( $fh ) . "\n";

close $fh;
}
else
{
foreach $file ( @{ $args->{ "FILES" } } )
{
$fh = &Common::read_open( $file );

$count = &count_entries( $fh );

print join( "\t", $file, $count ), "\n";

close $fh;

$tot += $count;
}
}
 
M

Mumia W.

G'day all,

In order to stream-line my scripts I would like to be able to read
data from either file(s) or a pipe, however, the way I do this results
in a lot of redundant code as shown in the below code snip. Somehow,
there must be a smarter way to do this!?!

Cheers,

M


if ( not -t STDIN )
{
$fh = &Common::pipe_open();

print &count_entries( $fh ) . "\n";

close $fh;
}
else
{
foreach $file ( @{ $args->{ "FILES" } } )
{
$fh = &Common::read_open( $file );

$count = &count_entries( $fh );

print join( "\t", $file, $count ), "\n";

close $fh;

$tot += $count;
}
}

Perhaps you are looking to read from the <> operator:

while (<>) {
... do something ...
}
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top