Click

Automatic detection of character encoding in terminal perl-script.

Tired of hand coding to change the normal output of the Russian text in your perl-script depending on the encoding of the terminal (run scripts under Linux (utf8), Windows (ActivePerl, cp866) and Windows ( Cygwin , cp1251)). Googling a bit, but there are no ready recipes are not met, a good query to find something in my head did not come.

I decided to do the following - to determine the operating system, in which the script runs, and depending on the encoding of the display output stream (STDOUT):

 #! / Usr / bin / perl; use utf8; require encoding; import encoding ('utf8', 'STDOUT' => & SetEncoding); sub SetEncoding {if ($ ^ O = ~ / ^ MSWin \ d {2} $ / i) {return 'cp866';} elsif ($ ^ O = ~ / ^ cygwin $ / i) {return 'cp1251';} elsif (condition for the next operating system (for example - linux)) {return 'utf8';}. ..  and so on, based on personal needs ....  } 

Comments, corrections, additions and / or other ways to solve the problem by installing an automatic encoding to perl-script are always welcome!

And just in case I suddenly can not find time to write something else to blog until the new year - congratulations to all my readers a Happy New Year, good luck and a little more pleasant discoveries! :)

PS: And for those interested in freelancing - a new, but rather an interesting resource FrilansFM - portal for freelance

More on similar topics:

Category Filed under: Coding , News | Tag Tags: , , , , , | Comments No Comments

Leave a Reply