Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How can I change the code page from within my perl code? I want to have the program switch to code page 1252. Of course, at a command prompt, I could just type 'chcp 1252' before running my program, but I would like to be able to start up my program by clicking on it, so that is not an option. I tried   system("chcp 1252");   but that seems to spawn a shell, change the code page there, and then exit, not changing the code page in the current shell. How can I get perl to change the current shell.
Thanks! GVW

Replies are listed 'Best First'.
Re: Changing code page from perl
by ikegami (Patriarch) on Sep 18, 2010 at 01:54 UTC
    $CONSOLE->InputCP(1252);

    Win32::Console

    [system("chcp 1252");] seems to spawn a shell, change the code page there

    While that's true, it doesn't matter. The OEM code page is a console setting, not a per-process setting.

    >perl -MData::Dumper -e"$Data::Dumper::Useqq=1; system('chcp 1252'); print(Dumper(`dir /b *.txt`)); system('chcp 437'); print(Dumper(`dir /b *.txt`));" Active code page: 1252 $VAR1 = "\355.txt\n"; Active code page: 437 $VAR1 = "\241.txt\n";
      Thank you! Win32::Console is just what I needed.
Re: Changing code page from perl
by bluescreen (Friar) on Sep 18, 2010 at 01:41 UTC

    If what's important to you is the input/output to the console you can use the enconding that will open STDIN/STDOUT/STDERR in the encoding you specify or Encode to use encode/decode functions manually.

    Now if you trying to change the encoding globally (outside Perl) I can't help you there

Re: Changing code page from perl
by Anonymous Monk on Sep 18, 2010 at 01:16 UTC
    From my understanding, you can't. If its at all possible, its using some super secret Win32::API calls which you would read about on MSDN