I get the post request with the data in the utf-8 encoding and write them in the database, but all the data in the database should be stored in cp1251 encoding, as a result, the data that comes in the post request and entered into the database will look like questions, how to change it in perl the encoding of the incoming data (or more simply, how to change the encoding of the string to cp1251)?

  • What kind of database? And then, perhaps, everything is solved simply by setting the connection encoding? ... - drdaeman
  • yes richem here ... everything is in order there, I need cp1251 encoding, I have everything in cp1251 encoding, except for the data that is transmitted by the request, that's what I need to transcode to cp1251 - shol
  • DB while converting utf8 to 1251 can lead to data loss. - user6550
  • just if I have the file encoding (primer.pl) utf-8, then the error is Internal Server Error - shol
  • This is about something else at all ... - user6550

3 answers 3

There is a module Encode and its function of the same name encode. Convert the string to cp-1251 and only after add it to the database. Check what encoding matches the data in the database; it should also be specified as cp-1251. And yes, also check the mysql_enable_utf flag; when connecting to the database from the script, it can be forced to be raised, which is not necessary in your case.

  • Encode :: encode () cannot be used for transcoding, it is for something completely different. - user6550

And what will you do with characters missing in CP1251? Maybe it's better to still store data in a database in UTF-8?

If the base is MySQL, then the encoding can be performed by means of the DBMS itself: MySQL (if it is not ancient 3.23. *) Allows you to store data in one encoding (and in CP1251), and communicate between the server and the client in another. You can switch the encoding in which data is transmitted in various ways, including the SET NAMES command:

 -- Передаваемые данные — в UTF-8 SET NAMES utf8; 

    Text::Iconv , Convert::Cyrillic , even Encode::from_to fit, a lot of options. But this is a very bad idea :(