Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: Capture a non-printable char and test what it is

by almsdealer (Acolyte)
on May 21, 2022 at 22:02 UTC ( #11144060=note: print w/replies, xml ) Need Help??


in reply to Re: Capture a non-printable char and test what it is
in thread Capture a non-printable char and test what it is

How does \e represent the escape key? Where do you find that?

If I look at the ascii table I see that the escape key can be represented as: 27 (decimal), 1B (hexadecimal), 033 (octal). But I am not sure how to test whether the value returned from ReadKey is one of those.

  • Comment on Re^2: Capture a non-printable char and test what it is

Replies are listed 'Best First'.
Re^3: Capture a non-printable char and test what it is
by haukex (Archbishop) on May 22, 2022 at 04:41 UTC
    Where do you find that?

    kcott is right that I simply looked at the code's output, but it's also documented in Quote and Quote like Operators.

    If I look at the ascii table I see that the escape key can be represented as: 27 (decimal), 1B (hexadecimal), 033 (octal).

    Those work too: the string "\e" is the same as "\x1B" and "\033" - you can try this out yourself with eq.

      Thank you, that link to quote like characters is very useful. I think one mistake I made was trying to test if the char was the hex value of the escape key using single quotes.

        In the time-honoured tradition of TMTOWTDI, if you replace

        if ( $key eq "\e" ) { print "Escape pressed\n"; }

        with

        if ( $key eq "\e" && $key eq "\x1b" && $key eq "\033" && $key eq "\c[" && $key eq "\x{1b}" && $key eq "\o{33}" && $key eq "\N{ESCAPE}" && $key eq "\N{U+001B}" && $key eq v27 && ord($key) == 27 && ord($key) == 0x1b && ord($key) == 033 && ord($key) == 0o33 && ord($key) == 0b11011 ) { print "Escape pressed\n"; }

        you'll get the same result.

        I don't claim that's a non-exhaustive list of all possibilities, but I think it's pretty close.

        You can find references to all of those in one or more of: perlop; perldata; perlrebackslash.

        — Ken

Re^3: Capture a non-printable char and test what it is
by kcott (Archbishop) on May 22, 2022 at 03:26 UTC
    "How does \e represent the escape key? Where do you find that?"

    If you'd bothered to run the short piece of code ++haukex provided, and pressed the escape key, you would have seen:

    Press a key $VAR1 = "\e"; Escape pressed

    Had you pressed the enter key:

    Press a key $VAR1 = "\n";

    I suggest you also read ++haj's post for additional information and alternative methods to use.

    — Ken

      Yes, my mistake. I see that now.
Re^3: Capture a non-printable char and test what it is
by cavac (Priest) on May 24, 2022 at 14:56 UTC

    If I look at the ascii table

    then you only see a part of the non-printable characters available on modern computer systems. Unicode has more control characters, emoji skin tone modifiers, right-to-left mark and a host of other stuff that is unprintable on it's own.

    As an additional bonus, the same character on screen can sometimes be encoded in Unicode in multiple ways, see Unicode equivalence.

    Unfortunately, input processing has gotten a tad more complex since the world gave up on ye olde ASCII table. On the bright side, these days more than the 20% of world population of the old ASCII days can now type their name into a computer with a reasonable expectation that it will be processed correctly.

    perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'

      I generally agree with everything you've written there; however, as a minor nitpick, those skin tone modifiers can be printed in isolation. I'm not sure how this will render on different browsers, but on my terminal:

      $ perl -C -E '
          say for
              "\N{U+1F3FB}",
              "\N{U+1F3FC}",
              "\N{U+1F3FD}",
              "\N{U+1F3FE}",
              "\N{U+1F3FF}"
      '
      🏻
      🏼
      🏽
      🏾
      🏿
      

      And, in a preview, that looks fine on my Firefox v100.0.2 — YMMV.

      — Ken

        You are right, those are printable when used standalone. Which makes them sometimes-printable-characters. Great, another exception that has to be handled when working with text.

        You are in the hallways of the text processing convention. To the sout +h, you see someone selling T-Shirts, to the north is the building exit. The entrance to the lecture hall is to the west. > complain about unicode cavac raises his fist to the gods and shouts "UNICODE!!!". Hr shoots + cavac in the buttocks with a mistletoe arrow.

        perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11144060]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2022-12-06 21:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?