views:

72

answers:

3

I am fairly new to Perl programming but I have a fair amount of experience with linux. Lets 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?

+1  A: 

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.

Matt Kane
+1  A: 

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
Sinan Ünür
Redirecting stdin to the executable's stdin is completely redundant or nonsensical.
Matt Kane
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?
Dalton Conley
Find the `/dev/INPUTDEVICE`. If this is a USB device, connect it and look at the output of `dmesg`
Sinan Ünür
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.
Dalton Conley
I saw the recommendation to use `pci=assign-busses`. See http://tldp.org/HOWTO/BootPrompt-HOWTO-4.html
Sinan Ünür
nothing on pci=assign-busses .. any other suggestions? I've been working on this for about 2 weeks, and cant figure out how to read from this reader!
Dalton Conley
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.
brian d foy
+1  A: 

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.

brian d foy
What does "the standard ERROR file descriptor" (emphasis mine) have to do with it?
Ben Voigt
Well, you could have just fixed the mistake instead of complaining about it. Editing is a core feature of StackOverflow :)
brian d foy