How do I detect if I have incoming data in <STDIN> when I pipe somethingto my perl script ?

Discussion in 'Perl Misc' started by Andreas Berg, Aug 5, 2004.

  1. Andreas Berg

    Andreas Berg Guest

    Greetings,

    I'm writing a script that is supposed to fetch information from <STDIN>
    that is piped to the script, something like this:
    > cat file.txt | myscript.pl


    With this, I will get what I want into <STDIN> and I can read it. But,
    this is the only case I want to read it. I do not want my script to try
    to read from a file in case I do have any command line paramters and I
    do not want the script to wait for keyboard input. If something is piped
    to the script, I want that data, if nothing is piped to the script, I
    dont want it to do anything with <STDIN>, how do I accomplish this ?

    regards,
    Andreas
     
    Andreas Berg, Aug 5, 2004
    #1
    1. Advertising

  2. Andreas Berg

    Sandman Guest

    Re: How do I detect if I have incoming data in <STDIN> when I pipe something to my perl script ?

    In article <cet6gg$5g5$>,
    Andreas Berg <> wrote:

    > Greetings,
    >
    > I'm writing a script that is supposed to fetch information from <STDIN>
    > that is piped to the script, something like this:
    > > cat file.txt | myscript.pl

    >
    > With this, I will get what I want into <STDIN> and I can read it. But,
    > this is the only case I want to read it. I do not want my script to try
    > to read from a file in case I do have any command line paramters and I
    > do not want the script to wait for keyboard input. If something is piped
    > to the script, I want that data, if nothing is piped to the script, I
    > dont want it to do anything with <STDIN>, how do I accomplish this ?
    >
    > regards,
    > Andreas


    cat file.txt | myscript.pl -stdin


    And have myscript.pl read from <STDIN> only if $ARGV[0] is "-stdin", or use
    getopts if you want more elaborate control of the parameters. This is, if I am
    reading this line correctly:

    "I do not want my script to try to read from a file in case I do
    have any command line paramters"

    I take that one as if you don't want to call your script with

    myscript.pl -f file.txt

    to read the file, and not to mean that you don't want to use parameters at all.

    --
    Sandman[.net]
     
    Sandman, Aug 5, 2004
    #2
    1. Advertising

  3. Andreas Berg

    Andreas Berg Guest

    Re: How do I detect if I have incoming data in <STDIN> when I pipesomething to my perl script ?

    The script will have command line parameters for other things and when I
    do not pipe something to the script, I dont want it to try to read these
    command line parameters as if they where a file.

    And, I never want the script to read from keyboard.

    Having -stdin as a command line parameter would work, but it is a dirty
    solution in my opinion. Doesn't look very nice.

    regards,
    Andreas

    Sandman wrote:

    > In article <cet6gg$5g5$>,
    > Andreas Berg <> wrote:
    >
    >
    >>Greetings,
    >>
    >>I'm writing a script that is supposed to fetch information from <STDIN>
    >>that is piped to the script, something like this:
    >> > cat file.txt | myscript.pl

    >>
    >>With this, I will get what I want into <STDIN> and I can read it. But,
    >>this is the only case I want to read it. I do not want my script to try
    >>to read from a file in case I do have any command line paramters and I
    >>do not want the script to wait for keyboard input. If something is piped
    >>to the script, I want that data, if nothing is piped to the script, I
    >>dont want it to do anything with <STDIN>, how do I accomplish this ?
    >>
    >>regards,
    >>Andreas

    >
    >
    > cat file.txt | myscript.pl -stdin
    >
    >
    > And have myscript.pl read from <STDIN> only if $ARGV[0] is "-stdin", or use
    > getopts if you want more elaborate control of the parameters. This is, if I am
    > reading this line correctly:
    >
    > "I do not want my script to try to read from a file in case I do
    > have any command line paramters"
    >
    > I take that one as if you don't want to call your script with
    >
    > myscript.pl -f file.txt
    >
    > to read the file, and not to mean that you don't want to use parameters at all.
    >
     
    Andreas Berg, Aug 5, 2004
    #3
  4. Re: How do I detect if I have incoming data in <STDIN> when I pipesomething to my perl script ?

    Andreas Berg wrote:
    > I'm writing a script that is supposed to fetch information from
    > <STDIN> that is piped to the script, something like this:
    > > cat file.txt | myscript.pl

    >
    > With this, I will get what I want into <STDIN> and I can read it.
    > But, this is the only case I want to read it. I do not want my
    > script to try to read from a file in case I do have any command
    > line paramters and I do not want the script to wait for keyboard
    > input. If something is piped to the script, I want that data, if
    > nothing is piped to the script, I dont want it to do anything with
    > <STDIN>, how do I accomplish this ?


    unless (eof STDIN) {
    my $data = do { local $/; <STDIN> };
    # process data
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 5, 2004
    #4
  5. Re: How do I detect if I have incoming data in <STDIN> when I pipe something to my perl script ?

    In article <cet96a$6it$>, "Andreas Berg"
    <> wrote:
    <SNIP>
    > The script will have command line parameters for other things and when I
    > do not pipe something to the script, I dont want it to try to read these
    > command line parameters as if they where a file.


    This will never happen, for exactly the reason you don't want it to
    happen - they are two entirely different things. CLI parameters appear in
    @ARGV, stdin appears on <STDIN>.
     
    Richard Gration, Aug 5, 2004
    #5
  6. Andreas Berg

    Guest

    Re: How do I detect if I have incoming data in <STDIN> when I pipe something to my perl script ?

    Gunnar Hjalmarsson <> wrote:
    > Andreas Berg wrote:
    > > I'm writing a script that is supposed to fetch information from
    > > <STDIN> that is piped to the script, something like this:
    > > > cat file.txt | myscript.pl

    > >
    > > With this, I will get what I want into <STDIN> and I can read it.
    > > But, this is the only case I want to read it. I do not want my
    > > script to try to read from a file in case I do have any command
    > > line paramters and I do not want the script to wait for keyboard
    > > input. If something is piped to the script, I want that data, if
    > > nothing is piped to the script, I dont want it to do anything with
    > > <STDIN>, how do I accomplish this ?

    >
    > unless (eof STDIN) {
    > my $data = do { local $/; <STDIN> };
    > # process data
    > }


    On my computer, 'eof STDIN' will, when run without STDIN redirection, block
    and wait for one line of user input on STDIN, so it doesn't do what the OP
    wants.

    In my hands, the file test "-p STDIN" works:

    $ perl -le 'print -p STDIN ? "Read STDIN" : "Dont read STDIN";'
    Dont read STDIN
    $ echo blah | perl -le 'print -p STDIN ? "Read STDIN" : "Dont read STDIN";'
    Read STDIN


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Aug 6, 2004
    #6
  7. Re: How do I detect if I have incoming data in <STDIN> when I pipesomething to my perl script ?

    wrote:
    > Gunnar Hjalmarsson wrote:
    >>
    >> unless (eof STDIN) {
    >> my $data = do { local $/; <STDIN> };
    >> # process data
    >> }

    >
    > On my computer, 'eof STDIN' will, when run without STDIN
    > redirection, block and wait for one line of user input on STDIN, so
    > it doesn't do what the OP wants.


    Hmm... So it seems.

    I tested the code in a CGI context, calling the script with or without
    POSTing data, and then it worked. Thought it would work from command
    line as well. Obviously not. :(

    Thanks!

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Aug 6, 2004
    #7
  8. Andreas Berg

    Andreas Berg Guest

    Re: How do I detect if I have incoming data in <STDIN> when I pipesomething to my perl script ?

    Richard Gration wrote:
    > In article <cet96a$6it$>, "Andreas Berg"
    > <> wrote:
    > <SNIP>
    >
    >>The script will have command line parameters for other things and when I
    >>do not pipe something to the script, I dont want it to try to read these
    >> command line parameters as if they where a file.

    >
    >
    > This will never happen, for exactly the reason you don't want it to
    > happen - they are two entirely different things. CLI parameters appear in
    > @ARGV, stdin appears on <STDIN>.


    Actually, if the first command line parameter is a file, you can read it
    using <STDIN>, or maybe its just <> you can read it with, but if I
    recall correctly, they are the same thing.

    regards,
    Andreas
     
    Andreas Berg, Aug 6, 2004
    #8
  9. Re: How do I detect if I have incoming data in <STDIN> when I pipe something to my perl script ?

    In article <cevblc$2ds$>, "Andreas Berg"
    <> wrote:
    > Actually, if the first command line parameter is a file, you can read it
    > using <STDIN>, or maybe its just <> you can read it with, but if I
    > recall correctly, they are the same thing.


    Right second time. The <> is some perl magic which treats the command
    line arguments as filenames and allows you to read their content via <>.
    This has nothing to do with stdin though, <> and <STDIN> are different
    beasts.

    #!/usr/bin/perl

    print 'Do you want to see the contents of <>? (y/n) ';
    chomp (my $ans = <STDIN>);

    if ($ans =~ /^y/i) {
    print for <>;
    } else {
    print "Bye bye!";
    }

    HTH
    Rich
     
    Richard Gration, Aug 6, 2004
    #9
  10. Andreas Berg

    gnari Guest

    Re: How do I detect if I have incoming data in <STDIN> when I pipe something to my perl script ?

    "Richard Gration" <> wrote in message
    news:cevs6s$jqo$2surf.net...
    > In article <cevblc$2ds$>, "Andreas Berg"
    > <> wrote:
    > > Actually, if the first command line parameter is a file, you can read it
    > > using <STDIN>, or maybe its just <> you can read it with, but if I
    > > recall correctly, they are the same thing.

    >
    > Right second time. The <> is some perl magic which treats the command
    > line arguments as filenames and allows you to read their content via <>.
    > This has nothing to do with stdin though, <> and <STDIN> are different
    > beasts.


    different beasts, but related.
    do not forget that in the abscence of filename arguments, <> reads
    from STDIN

    gnari
     
    gnari, Aug 6, 2004
    #10
  11. Andreas Berg

    Joe Smith Guest

    Re: How do I detect if I have incoming data in <STDIN> when I pipesomething to my perl script ?

    Andreas Berg wrote:

    > I do not want the script to wait for keyboard input.


    That's different than what you asked the the subject line.

    Q: How can I tell when data becomes available on STDIN?
    A: Use select() checking for input available on file handle 0.
    This is recommended when reading from a TCP/IP socket.

    Q: How can I tell when STDIN is associated with a pipe (or
    redirected from an input file) and not coming from the terminal?
    A: Use the -t() function.

    if (-t) {
    print "Input appears to be from a tty; not reading from it\n";
    @lines = ();
    } else {
    print "Reading from input file or pipe\n"
    @lines = <STDIN>;
    }
    print "Read ",scalar(@lines)," lines\n";

    -Joe
     
    Joe Smith, Aug 7, 2004
    #11
  12. Andreas Berg

    Joe Smith Guest

    Re: How do I detect if I have incoming data in <STDIN> when I pipesomething to my perl script ?

    wrote:

    > In my hands, the file test "-p STDIN" works:
    >
    > $ perl -le 'print -p STDIN ? "Read STDIN" : "Dont read STDIN";'
    > Dont read STDIN
    > $ echo blah | perl -le 'print -p STDIN ? "Read STDIN" : "Dont read STDIN";'
    > Read STDIN


    That works for input from a pipe, but not when redirecting input from a file.
    The file test "-t STDIN" handles both.
    -Joe
     
    Joe Smith, Aug 7, 2004
    #12
    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. dpackwood
    Replies:
    3
    Views:
    1,811
  2. lee, wonsun
    Replies:
    1
    Views:
    490
    Jack Klein
    Nov 2, 2004
  3. GerShar
    Replies:
    1
    Views:
    723
    Dennis Lee Bieber
    Oct 31, 2005
  4. Replies:
    1
    Views:
    227
    Ben Morrow
    Jun 2, 2004
  5. saeed
    Replies:
    2
    Views:
    211
    Joe Smith
    Sep 7, 2004
Loading...

Share This Page