How to read input data from pipe, file and files

Discussion in 'Perl Misc' started by maasha, Jul 9, 2007.

  1. maasha

    maasha Guest

    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;
    }
    }
     
    maasha, Jul 9, 2007
    #1
    1. Advertising

  2. maasha

    Mumia W. Guest

    On 07/09/2007 01:58 AM, maasha wrote:
    > 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 ...
    }
     
    Mumia W., Jul 9, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lee, wonsun
    Replies:
    1
    Views:
    499
    Jack Klein
    Nov 2, 2004
  2. j. del
    Replies:
    13
    Views:
    18,779
    Howard
    Mar 28, 2005
  3. Replies:
    4
    Views:
    984
    M.E.Farmer
    Feb 13, 2005
  4. Replies:
    1
    Views:
    240
    Ben Morrow
    Jun 2, 2004
  5. Gerhard

    Pipe input from a Text-File

    Gerhard, Dec 3, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    292
    Joe Smith
    Dec 4, 2004
Loading...

Share This Page