but that didn't work with [ESC q]. Instead I ended up having to use:$DataRead{'StringRead'} = substr($DataRead{'StringRead'}, -3);
This works fine, but leaves me wondering why the first approach didn't work. BTW Term::HotKey is just the module given in perlfaq8. I'm using perl v5.6.1 in on a Sun OS box using putty as an xterm. My test script follows:$DataRead{'StringRead'} = substr($DataRead{'StringRead'}, 0, length($D +ataRead{'StringRead'}) - 2);
#!/usr/bin/perl use strict; use warnings; use constant true => 1; use constant TAB => 9; use constant LF => 10; use constant CR => 13; use constant ESC => 27; use constant BACKTAB => -1; $| = true; use Term::HotKey; # # ReadFromKeyboard # # Read a key at Time from the Keyboard # # Arguments: # # Returns: \%DataRead # 'StringRead' => Characters Read # 'ExitValue' => Character That Caused End of Read # # Thanks to Liverpole for the Original Version # sub ReadFromKeyboard { my %DataRead; $DataRead{'StringRead'} = ''; my $CharacterRead = ''; while(true) { $CharacterRead = readkey(); if($CharacterRead) { $DataRead{'StringRead'} .= $CharacterRead; print("\[$CharacterRead\]\[" . ord($CharacterRead) . "\]len\[ +" . length($CharacterRead) . "\] \[$DataRead{'StringRead'}\]\n"); if($DataRead{'StringRead'} =~ /.*\t|\n|\r|\e\[Z|\eq$/) { if($DataRead{'StringRead'} =~ /.*\e\[Z$/) { $DataRead{'ExitValue'} = -1; $DataRead{'StringRead'} = substr($DataRead{'StringRead' +}, -3); } elsif($DataRead{'StringRead'} =~ /.*\eq$/) { $DataRead{'ExitValue'} = 27; $DataRead{'StringRead'} = substr($DataRead{'StringRead' +}, 0, length($DataRead{'StringRead'}) - 2); } else { $DataRead{'ExitValue'} = ord(chop($DataRead{'StringRead +'})); } last(); } } } return(\%DataRead); } # Main program my $DataRead; $DataRead->{'StringRead'} = ''; while(uc($DataRead->{'StringRead'}) ne 'QUIT') { $DataRead = ReadFromKeyboard(); my $WhatIsIt; if($DataRead->{'ExitValue'} == TAB) { $WhatIsIt = 'TAB'; } elsif($DataRead->{'ExitValue'} == LF) { $WhatIsIt = 'LF'; } elsif($DataRead->{'ExitValue'} == CR) { $WhatIsIt = 'CR'; } elsif($DataRead->{'ExitValue'} == ESC) { $WhatIsIt = 'ESC'; } elsif($DataRead->{'ExitValue'} == BACKTAB) { $WhatIsIt = 'BACKTAB'; } else { $WhatIsIt = 'IDK'; } print("You Entered: \[$DataRead->{'StringRead'}\]\[" . length($Data +Read->{'StringRead'}) . "\]\nYou Ended with a \[$WhatIsIt\]\[$DataRea +d->{'ExitValue'}\]\n"); } __END__
In reply to Strange substr behavior by NateTut
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |