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

How do I stop nasty escape characters from being printed? For example.
while(1) { $x = <STDIN>; print "$x\n"; }
If you hit any of the arrow keys aweful escape characters get printed. Also how do you detect when an arrow key has been hit? And last but not least when you compile a script with pp is it decompilable?(And if it is not is it platform-independent?) Also pp compiled perl seems to have an overhead. Is it a one time only overhead or does it get bigger the more code you have?

Replies are listed 'Best First'.
Re: Escape characters and arrow keys and pp
by Khen1950fx (Canon) on Nov 12, 2011 at 07:55 UTC
    I can't tell what you mean by 'pp' and 'decompilable', but I think that you were thinking of something like this to avoid printing the arrow keys:
    #!/usr/bin/perl -l use strict; use warnings; use Term::ReadKey; while (1) { print "Enter something:"; ReadMode(4); my $x = <STDIN>; print "Entering..."; sleep 3; ReadMode(0); print "You entered: $x"; last; }
      Thanks. This thread is solved.
Re: Escape characters and arrow keys and pp
by Marshall (Canon) on Nov 12, 2011 at 09:37 UTC
    If what you mean by "decompilable" that folks can read your source code, then yes they can do that. With ActiveState's PerlApp, the source code is encrypted somewhat.

    If you are building standalone executable files, then there is a big overhead to get Perl itself to begin with (there is a minimum size). The incremental growth rate depends upon how much source code that you include for Perl to compile and work with. That includes your code and modules that you "use".

    This is not a "compile", it is more like packaging the Perl executable engine and the code that you need together as one unit so that it does not require Perl to already be resident. It is not platform independent like a Java jar file.

Re: Escape characters and arrow keys and pp
by afoken (Chancellor) on Nov 12, 2011 at 18:27 UTC
    when you compile a script with pp ...

    pp does not compile perl code. It wraps (hopefully) all required modules, the main script, and the perl interpreter into a single binary. Think of it as a self-extracting archive that automatically starts the main script after unpacking, using the embedded perl interpreter.

    ... is it decompilable?

    Yes. See Uncool Use Of Perl: perl2exe. decompile quick steps, Re: Protection for Perl Scripts, Re^2: best way to store login information for a perl script?, Re^2: Where should I have configuration information in a file or database. Last but not least, pp clearly documents that it uses PAR as its archive format, which is a renamed, unprotected ZIP file.

    (And if it is not is it platform-independent?)

    No. Compiled binaries are never platform-independent. Typically, it works only on the same platform. Some platforms have ways to execute code written for older platforms (e.g. most Linux x64 systems can run Linux x86 executables, same for Win16 on Win32 and Win32 on Win64, MacOS classic apps on PowerPC MacOS X versions) or for other platform (FreeBSD can emulate the Linux API, Linux can run some Windows executables using wine). But you can't expect any compiled binary to run on any platform.

    Also pp compiled perl seems to have an overhead. Is it a one time only overhead or does it get bigger the more code you have?

    Since pp must contain all required code, the final executable grows bigger and unpacking takes more time.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Escape characters and arrow keys and pp
by ww (Archbishop) on Nov 12, 2011 at 12:23 UTC
    7cardcha: Welcome to the Monastery.

    ++ for formating the code correctly... but - - - in part for posting at least two unrelated questions in the same SOPW... and for the pointless, overstated or ambiguous use of "nasty" in part 1 of question 1.

    But the real reason for the (offsetting1 downvote is the hope that it will prod you to read On asking for help and How do I post a question effectively? and to start familiarizing yourself with the perldocs available on your own machine. Run perldoc perltoc there are see perltoc. And, not just BTW, googling this site with appropriate keywords related to your question will often spare you the wait for an answer.

    You've received very decent answers to your questions; you'll continue to get those here as you progress to more complex issues... if you show a bit more effort before asking others to dedicate their time to your problems.

    1 IOW, no upvote or downvote cast; mentioned as instructional points, only.

Re: Escape characters and arrow keys and pp
by Plankton (Vicar) on Nov 12, 2011 at 05:18 UTC
    You mean like this ...
    #!/usr/bin/perl -w use strict; while(1) { while(<>) { s/\W//g; print; } }
      Yes. Please explain the s/\W//g; part