Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by kcott (Archbishop)
on May 23, 2022 at 07:32 UTC ( #11144112=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^6: Capture a non-printable char and test what it is
by afoken (Canon) on May 23, 2022 at 22:10 UTC
    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"; }

    I wonder how much of it remains after parsing and optimizing:

    #!perl use strict; use warnings; use charnames qw( :full ); for my $key ("r", "x", "\e", "z") { 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 # that's not perl: && ord($key) == 0o33 && ord($key) == 0b11011 ) { print "Escape pressed\n"; } }
    X:\>perl -MO=Deparse 11144112.pl use charnames (':full'); use warnings; use strict 'refs'; BEGIN { $^H{'charnames_inverse_ords'} = q(HASH(0x2a62c30)); $^H{'charnames_stringified_inverse_ords'} = q(); $^H{'charnames'} = q(CODE(0x2b77d90)); $^H{'charnames_full'} = q(1); $^H{'charnames_scripts'} = q(); $^H{'charnames_name_aliases'} = q(HASH(0x2a62c18)); $^H{'charnames_ord_aliases'} = q(HASH(0x2a62c90)); $^H{'charnames_short'} = q(0); $^H{'charnames_stringified_names'} = q(); $^H{'charnames_stringified_ords'} = q(); } foreach my $key ('r', 'x', "\e", 'z') { if ($key eq "\e" and $key eq "\e" and $key eq "\e" and $key eq "\e +" and $key eq "\e" and $key eq "\e" and $key eq "\e" and $key eq "\e" + and $key eq v27 and ord $key == 27 and ord $key == 27 and ord $key = += 27 and ord $key == 27) { print "Escape pressed\n"; } } 11144112.pl syntax OK X:\>perl -v This is perl 5, version 14, subversion 2 (v5.14.2) built for MSWin32-x +64-multi-thread Copyright 1987-2011, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using "man perl" or "perldoc perl". If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge. X:\>

    So, clearly all variants of writing "\e" and 27 were unified, but I kind of expected that the optimizer would eliminate repeated identical expressions. Like this:

    if ($key eq "\e" and $key eq v27 and ord $key == 27) { print "Escape pressed\n"; }

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      G'day Alexander,

      My intention was to show the variety of alternatives available. I would never expect the "... && ... && ..." to be used in a real program.

      "# that's not perl: && ord($key) == 0o33"

      Actually it is; however, it's very new. See "perl5340delta: New octal syntax 0oddddd".

      I have Perl v5.34.0 installed. I checked my code with "perl -e 'use strict; use warnings; ...'" which would normally pick up things introduced since v5.8 (e.g. say). It's good to know that 0o33 passed on my installation without including a "use 5.034;" statement; I'll keep an eye on that in other code. Thanks for spotting this.

      — Ken

        For changes not influenced by feature there's my Syntax::Construct (the link goes directly to the 0o section).

        Update: Fixed a typo, thanks Discipulus.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        I would never expect the "... && ... && ..." to be used in a real program.

        Not in the form shown here. But maybe in some degenerated cases in generated code.

        "# that's not perl: && ord($key) == 0o33"

        Actually it is; however, it's very new. See "perl5340delta: New octal syntax 0oddddd".

        Interesting. Redundant, as a leading zero already indicates octal since decades, no "o" required. And a nice complement to 0x and 0b.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2022-08-13 06:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?