perllearner has asked for the wisdom of the Perl Monks concerning the following question:

hello perl gurus..
i have a short script i need to run to grab barcode scans into a variable which then checks if the scanned input is a number etc. what i need is to scan the barcode and get a carriage return automatically. also the auto CR needs to be done only if the STDIN is != "" and if there is a 1/2 sec delay after the scan so that no more accidental scans will be accepted..

following is my wild shot at it.. which obviously doesnt work..

print "Scan in Bar Code: "; if(<STDIN> == "") { print "\n"; } else { sleep(1/2); print "\r"; }

Replies are listed 'Best First'.
Re: getting an auto carriage return on STDIN
by matija (Priest) on Mar 15, 2004 at 15:43 UTC
    So, basicaly you need to read whatever numbers come down the line quickly, even if they don't come with a <CR>, and then you have to ignore the text for a while.

    I'd use something like this (warning: untested)

    use Term:ReadKey; $str=""; while (need_more_data($str)) { $str.=ReadKey(0);} $bad_data=ReadKey(0.5); # fractional values are accepted here
    Note that with barcodes you usualy know if you should be receiving any more data or not: AFAIK, most barcodes for any given purpose are of a constant length.
Re: getting an auto carriage return on STDIN
by Limbic~Region (Chancellor) on Mar 15, 2004 at 15:38 UTC
    perllearner,
    I am not sure I understand completely, but I think the following does what you want:
    print "Scan in Bar Code: "; my $bar_code = <STDIN>; chomp $bar_code; if ( $bar_code ) { print "\n"; } else { select(undef, undef, undef, .5); print "\r"; }
    If this isn't what you want you are going to have to be more clear. You should also use CODE tags for your code so you get proper indentation.

    Cheers - L~R

Re: getting an auto carriage return on STDIN
by Abigail-II (Bishop) on Mar 15, 2004 at 15:41 UTC
    What you want is quite unclear. What do you mean by "get a carriage return"? The code seems to issue a carriage return, or a newline. Note also that <STDIN> usually won't return a newline. Normally, a read in line will be terminated by the line terminator. At end of file, this might be different, but in that case, use eof.

    Abigail