0

I am fairly new to Perl programming, but I have a fair amount of experience with Linux. Let’s say I have the following code:

while(1) {
    my $text = <STDIN>;
    my $text1 = <STDIN>;
    my $text2 = <STDIN>;
}

Now, the main question is: Does STDIN in Perl read directly from /dev/stdin on a Linux machine or do I have to pipe /dev/stdin to the Perl script?

2
  • 2
    What happened when you tried? Commented Sep 24, 2010 at 15:57
  • Did you mean to ask about /dev/console? Commented Sep 24, 2010 at 16:18

3 Answers 3

2

If you don't feed anything to the script, it will sit there waiting for you to enter something. When you do, it will be put into $text and then the script will continue to wait for you to enter something. When you do, that will go into $text1. Subsequently, the script will once again wait for you to enter something. Once that is done, the input will go into $text2. Then, the whole thing will repeat indefinitely.

If you invoke the script as

$ script < input

where input is a file, the script will read lines from the file similar to above, then, when the stream runs out, will start assigning undef to each variable for an infinite period of time.

AFAIK, there is no programming language where reading from the predefined STDIN (or stdin) file handle requires you to invoke your program as:

$ script < /dev/stdin
Sign up to request clarification or add additional context in comments.

7 Comments

Redirecting stdin to the executable's stdin is completely redundant or nonsensical.
well what Im trying to do is read input from a magnetic card swipe, which basically acts as a keyboard, unfortunately I can't seem to find the /dev/INPUTDEVICE file to read directly from, so I'm trying other options (STDIN) .. any suggestions?
Find the /dev/INPUTDEVICE. If this is a USB device, connect it and look at the output of dmesg
when I plug it in I get GIT USB READER AS /class/input/input5 .. I can go to /sys/class/input/input5 which is like a never ending directory tree, and I have no idea what to do with it.
Ya know, telling us what you were trying to do would have saved people a lot of hassle. You should add the bit about the magnetic card swipe to your question.
|
1

It reads directly from the STDIN file descriptor. If you run that script it will just wait for input; if you pipe data to it, it will loop until all the data is consumed and then wait forever.

You may want to change that to:

while (my $test = <STDIN>) {
   # blah de blah
}

so an EOF will terminate your program.

Comments

1

Perl's STDIN is, by default, just hooked up to whatever the standard input file descriptor is. Beyond that, Perl doesn't really care how or where the data came from. It's the same to Perl if you're reading the output from a pipe, redirecting a file, or typing interactively at the terminal.

If you care about each of those situations and you want to handle each differently, then you might try different approaches.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.